Search code examples
node.jstypescriptnestjs

Why are TypeScript build files being generated in /dist instead of /dist/src in my NestJS project?


I'm encountering a build configuration issue in my NestJS project using TypeScript. I expect the files inside the src folder to be built into dist/src, but currently, all files are being generated directly in the dist root. This problem started occurring after upgrading to TypeScript 3.x, whereas it was working as expected with TypeScript 2.x.

Here are my tsconfig.json and nest-cli.json configurations:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
  },
  "watchOptions": {
    "watchFile": "fixedPollingInterval",
    "watchDirectory": "useFsEvents",
    "fallbackPolling": "dynamicPriority",
    "synchronousWatchDirectory": true,
    "excludeDirectories": [
      "**/node_modules",
      "dist"
    ]
  }
}


{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true
  }
}

I've tried adjusting various settings, but the build result remains the same. Could this be related to how TypeScript 3 handles project references or the way it transpiles into the output directory? I'm looking for a way to have my src folder contents mirrored into dist/src just like it was with TypeScript 2.

Any insights or suggestions on how to configure TypeScript to achieve this would be greatly appreciated. Thank you!


Solution

  • Just change the outDir value in tsconfig.json

    "outDir": "./dist/src",
    

    You can also use command line option

    tsc --outDir ./dist/src