111

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.

aryzing
  • 4,982
  • 7
  • 39
  • 42

8 Answers8

214

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.

Saravana
  • 37,852
  • 18
  • 100
  • 108
35

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.

fregante
  • 29,050
  • 14
  • 119
  • 159
  • Was extending [ts-config-single-spa](https://github.com/single-spa/create-single-spa/blob/main/packages/ts-config-single-spa/tsconfig.json), which has `noEmit: true`. – AleksG Aug 13 '21 at 19:08
  • 1
    I added "noEmit": false and all finally my outDir was created and populated – Dustin Feb 18 '22 at 18:40
  • Bravo. Saved me. Thanks.In my case a tsconfig.json file had been misconfigured. The symptom was loads of errors reported by 'react-scripts start', but none reported by 'npx tsc'. – user2809286 Mar 15 '23 at 20:45
14

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.

joshweir
  • 5,427
  • 3
  • 39
  • 59
  • 1
    Thanks for deciding to post here! I couldn't figure out why `tsc -b` would only create my output folder occasionally. Turns out my `yarn:clean` would only `rm -rf packages*/dist`, leaving behind the `.tsbuildinfo` files! – Pakman Jan 26 '20 at 04:25
  • Thanks for this. Note that all composite projects are incremental. I had to, similar to above commenter, rm -rf *.tsbuildinfo as a prebuild step in my build project – Colin D Jun 18 '22 at 22:49
8

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

chandoo
  • 1,276
  • 2
  • 21
  • 32
4

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

cor blk
  • 41
  • 1
1

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/**/*"
  ],
}
Rohit
  • 6,365
  • 14
  • 59
  • 90
0

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

tsc --build mocks/tsconfig.json
Ash Blue
  • 5,344
  • 5
  • 30
  • 36
-1

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '22 at 07:07