网站搜索

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


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

我的项目树:

文件 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 版本:消息 TS6029:版本 1.7.5

详细错误:

C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31084 var jsonOptions=json["compilerOptions"]; ^TypeError: 无法读取未定义的属性 'compilerOptions' 在 getCompilerOptions (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31084:35) 在 Object.parseJsonConfigFileContent (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31074:22) 在 parseConfigFile (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc. js:31351:40) 在 performCompilation (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31362:45) 在 Object.executeCommandLine (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31336:9) 在 Object. (C:\Users\User\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js:31635:4) 在 Module._compile (module.js:435:26) 在 Object.Module._extensions..js (module.js:442:10) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:311:12)

答案#1

当 Visual Studio Code 运行 TypeScript 编译器时,它是相对于根文件夹运行的,但你的 tsconfig.json 实际上在 ./server 文件夹中。这就是编译器无法在根目录找到你的 tsconfig.json 文件的原因。你需要做的只是,在你的 tasks.json 中,需要将 args 参数更新为如下所示,假设你的整个项目都在 server 文件夹中。也就是说,你在 client 文件夹中没有需要由 TypeScript 编译器编译的 .ts 文件。

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

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