nest: tsconfig paths not working when building for production

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When using Typescript paths in tsconfig, the project can be built with nodemon, but not with tsc (with npm run start:prod).

Expected behavior

I would expect the same code to work regardless its being built for development or production.

Minimal reproduction of the problem with instructions

  1. Create a repo with Nest CLI
  2. Add a path to tsconfig.json (for example @foo):
"paths": {
  "@foo/*": [
    "./*"
  ]
}
  1. Change an import in app.module.ts: import { AppController } from '@foo/app.controller';
  2. Run npm run start:prod

What is the motivation / use case for changing the behavior?

I had to remove every path alias in my code to get my project working for production. Since tsconfig paths are a default dependency of the project, and configured to work with nodemon, I believe it should also work when building for production.

Environment


Nest version: 5.1.0

 
For Tooling issues:
- Node version: 10.8.0  
- Platform: Windows 

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@marcus-sa According to the docs of tsconfig-paths it can be used when executing the built sources with node. The issue comment you are referencing also mentions execution with node.

Actually the problem occurs when executing the built files with node dist/main.js, not during the build process with tsc.

EDIT: I managed to get a working solution by following theses instructions from tsconfig-paths docs. I created an example repo here.

  1. Add a file named tsconfig-paths-bootstrap.js to the root of the project, and paste the code from the docs in it.
  2. Change the start:prod script to: node -r ./tsconfig-paths-bootstrap.js dist/main.js
  3. You should now be able to compile and execute the project with TS paths. (be aware that you will not be able to compile if there is no path set in tsconfig.json)

@kamilmysliwiec I believe this should be referenced in Nest README/docs, and maybe added to the CLI generator, and example project. What’s your opinion on this ?

I had to replace ts extensions by js extensions in the paths.

const tsConfig = require("./tsconfig.json");
const tsConfigPaths = require("tsconfig-paths");

let { baseUrl, paths } = tsConfig.compilerOptions;
for (path in paths) {
  paths[path][0] = paths[path][0]
    .replace("src", "dist")
    .replace(".ts", ".js");
}

tsConfigPaths.register({ baseUrl, paths });

Hey for anybody who has this issue and could not fix it via the fix that @bisonfoutu posted. If you are using TypeORM, make sure that your entities & subscribers are set correctly there.

I could not get the fix to work and saw that a subscriber.ts was loaded which of course throws an error when executed inside a js-environment. So what I did to get rid of the problem was setting the entities & subscribers-folders inside the ormconfig.js dynamically depending on the NODE_ENV.

example:

const entityPath = process.env.NODE_ENV === 'dev' ? `${__dirname}/src/**/*.entity.ts` : `${__dirname}/dist/**/*.entity.js`;

tsconfig-paths only works inline (during execution) and not when building. I’d suggest using Webpack with awesome-typescript-loader and tsconfig-paths-webpack-plugin instead to build your application.

The example should be updated tho.

https://github.com/TypeStrong/ts-node/issues/138#issuecomment-269760415

@kamilmysliwiec how about i update the starter project with paths included and send a PR

@bisonfoutu : nice ! Just a note for those who read: when you paste the code from the link, DON’T copy the cleanup() call, cause it actually cancel the registration you just did. Just somewhat not clear to have put it in the same block here, could cause one to look around for a while…

Something is not right here as tsc only builds ts files and should do so without any changes to the config. tsconfig-paths/register is needed only for runtime for nodejs/nodemon. Judging by the example you provided earlier, could you ommit extensions from tsconfig path definitions and test this out? E.g replace @/**/*.ts to @/**/*.

Thanks for sharing your solution @bushybuffalo. If you have some time, you can create a PR to the starter project 😃