在 tsconfig.json 中的 typecript outDir 设置不起作用

package.json中使用时,我似乎无法让 outDir标志工作。目录结构非常简单: 根目录级别的 tsconfig.json、一个 src/目录、一个 index.ts 文件以及表示其他模块的其他目录。

在索引文件上运行 tsc命令时,它会在其旁边而不是在生成目录中创建一个新的。我做错了什么?

我的天啊:

{
"compilerOptions": {
"outDir": "build"
}
}

我的 npm 构建脚本:

"build": "tsc src/index.ts"

我从项目的根目录调用脚本。有趣的是,使用 --outDir标志运行相同的脚本就可以了。

60612 次浏览

When you pass in files for compilation with tsc src/index.ts, your tsconfig.json is ignored.

From the documentation:

When input files are specified on the command line, tsconfig.json files are ignored.

Your npm build script should just be tsc without passing any files.

This is my folder structure.

enter image description here

Keep the typescript files in src folder and keep the tsconfig.json in root.

In tsconfig json file add foldername for outDir in compilerOptions

"compilerOptions": {
"outDir": "build",
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"sourceMap": true
},

and run the below commands.

just cd to the root folder and type

tsc

or

tsc --outDir .

enter image description here

which will build the outDir folder with js and map.js files.

source: https://github.com/Microsoft/TypeScript/issues/10585

If you are using the incremental compiler option, you may not be getting output if you have deleted / modified files in your outDir but have not removed the .tsbuildinfo file.

My issue was a bit different, but Google brought me here - so figured others may also.

You need to declare your tsconfig file location instead of the file you want to build.

tsc --build mocks/tsconfig.json

In my case it was being ignored because I had noEmit: true in tsconfig.json. For whatever reason, the files still were emitted, but in the same directory instead of following outDir.

The config file was read correctly and this error also appeared when using the flag.

Make sure "outDir" is defined under "compilerOptions"

I had it defined at the same level as "compilerOptions"

{
"compilerOptions": {
"baseUrl": "node_modules/@types",
"lib": [
"es6"
],
"outDir": "dist",
"esModuleInterop": true,
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*"
],
}

Maybe I met the same problem. Your should set the script as "build": "tsc" rather than "build": "tsc src/index.ts"

This worked for me tsc --outDir dist/ where dist is my output or destination folder