tsconfig paths not working when building for production
See original GitHub issueI’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
- Create a repo with Nest CLI
- Add a path to tsconfig.json (for example
@foo):
"paths": {
"@foo/*": [
"./*"
]
}
- Change an import in
app.module.ts:import { AppController } from '@foo/app.controller'; - 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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:17 (5 by maintainers)
Top Results From Across the Web
Why are these tsconfig paths not working? - Stack Overflow
I did a bit of digging and found that the tryPaths array produced by tsconfig-paths has absolute URLs relative to the project /cwd...
Read more >Setting up Path Alias in TypeScript and tsc build without error
Third, go to tsconfig.json and add the paths alias options inside compilerOptions. ... Please be reminded that all paths in Paths are related...
Read more >Why TypeScript Paths Failed Me | Mitchell Simoens Blog
Run a build, test the compiled code and you'll still see the same "Cannot find module" error. The reason for this is TypeScript...
Read more >tsconfig-paths - npm
The typescript compiler can resolve these paths from tsconfig so it will compile OK. But if you then try to execute the compiled...
Read more >TSConfig Reference - Docs on every TSConfig option
This does not affect how TypeScript emits JavaScript, it only emulates the assumption that they will be able to work via those relative...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@marcus-sa According to the docs of
tsconfig-pathsit 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 withtsc.EDIT: I managed to get a working solution by following theses instructions from
tsconfig-pathsdocs. I created an example repo here.tsconfig-paths-bootstrap.jsto the root of the project, and paste the code from the docs in it.start:prodscript to:node -r ./tsconfig-paths-bootstrap.js dist/main.jstsconfig.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.