【问题标题】:vscode typescript Cannot read property 'compilerOptions' of undefinedvscode typescript无法读取未定义的属性'compilerOptions'
【发布时间】:2016-05-07 04:49:16
【问题描述】:

我正在尝试通过查看这个问题Visual Studio Code: compile typescript module 和这篇文章https://cmatskas.com/typescript-and-vs-code/ 来配置 vscode 来编译打字稿,但出现错误。需要帮助。

我的项目树:

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)

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2021-11-20
      • 2018-03-13
      • 2018-08-30
      • 1970-01-01
      • 2017-11-02
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多