vscode typescript无法读取属性' compilerOptions'未定义的

时间:2016-01-29 12:36:28

标签: windows typescript windows-10 visual-studio-code

我正在尝试通过查看此问题Visual Studio Code: compile typescript module和本文https://cmatskas.com/typescript-and-vs-code/来配置vscode以编译typescript,但是收到错误。需要帮助。

我的项目树:

enter image description here

文件server.ts这是我想编译的内容,但稍后我会有更多.ts个文件。

这是我的tasks.json

{
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "tsc",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur.
    "showOutput": "always",

    "windows": {
        "command": "tsc"
    },

    // args is the HelloWorld program to compile.
    "args": ["-p", "."],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

和我的tsconfig.json

{ "compilerOptions": { "target": "ES5", "module": "commonjs", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, "sourceRoot": "/" }, "exclude": [ "node_modules" ] }

我的tsc version:消息TS6029:版本1.7.5

详细错误:

C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31084 var jsonOptions = json["compilerOptions"]; ^ TypeError: Cannot read property 'compilerOptions' of undefined at getCompilerOptions (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31084:35) at Object.parseJsonConfigFileContent (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31074:22) at parseConfigFile (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31351:40) at performCompilation (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31362:45) at Object.executeCommandLine (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31336:9) at Object.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31635:4) at Module._compile (module.js:435:26) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12)

1 个答案:

答案 0 :(得分:2)

当Visual Studio代码运行typescript编译器时,它相对于根文件夹运行,但您的tsconfig.json实际上位于./server文件夹中。这就是编译器无法在根目录中找到tsconfig.json文件的原因。您需要做的就是在您的tasks.json中,您需要更新args参数,如下所示,假设您的整个项目位于服务器文件夹。即客户端文件夹中没有任何需要由typescript编译器编译的.ts文件。

// args is the HelloWorld program to compile.
"args": ["-p", "./server"],

如果您的客户端文件夹还有需要编译的ts文件,那么您需要将tsconfig.json移动到根文件夹,并且typescript编译器将能够找到它。