typescript outDir setting in tsconfig.json not working

久未见 提交于 2019-12-12 09:29:35

问题


I can't seem to get the outDir flag working when used in package.json. Directory structure is pretty simple: tsconfig.json at the root level, together with a src/ directory and a single index.ts file plus other directories representing other modules.

When running the tsc command on the index file, it creates a new one beside it instead of in the build directory. What am I doing wrong?

My tsconfig:

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

My npm build script:

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

I'm calling the script from the root dir of the project. Interestingly, running the same script with an --outDir flag works just fine.


回答1:


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.




回答2:


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.




回答3:


This is my folder structure.

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 .

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

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




回答4:


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

tsc --build mocks/tsconfig.json


来源:https://stackoverflow.com/questions/45661027/typescript-outdir-setting-in-tsconfig-json-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!