-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tsconfig paths not working when building for production #986
Comments
The example should be updated tho. |
@marcus-sa According to the docs of Actually the problem occurs when executing the built files with EDIT: I managed to get a working solution by following theses instructions from
@kamilmysliwiec I believe this should be referenced in Nest README/docs, and maybe added to the CLI generator, and example project. |
Thanks for sharing your solution @bushybuffalo. If you have some time, you can create a PR to the starter project :) |
@bisonfoutu : nice ! |
Hey for anybody who has this issue and could not fix it via the fix that @bisonfoutu posted. I could not get the fix to work and saw that a example:
|
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 }); |
Thank you for your ideas. I've managed to solve my problem in a bit more universal fashion. Here's my tsconfig.json {
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "dist",
"baseUrl": "src",
"paths": {
"@/*": ["./*"]
},
"incremental": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
} And tsconfig-paths-bootstrap.js const tsConfig = require('./tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');
const paths = tsConfig.compilerOptions.paths;
tsConfigPaths.register({
baseUrl: tsConfig.compilerOptions.outDir,
paths: Object.keys(paths).reduce(
(agg, key) => ({
...agg,
[key]: paths[key].map(p =>
p.replace(tsConfig.compilerOptions.baseUrl, tsConfig.compilerOptions.outDir),
),
}),
{},
),
}); This just sets baseDir to be destination and replaces base dir with destination dir inside every path as well. Hope this helps someone. |
@psabeckis How do you handle the file extensions (ts VS js) ? |
@lapwat I may not understand the problem you have, but the main problem I see here is that tsc does not support absolute imports this approach fixes that. You don't have to specify extensions in paths and since node is running the js files after build the only thing you need to worry about is the absokute path resolution. If you could provide a usecase I would be happy to help out if I can. |
@psabeckis That is why I have to change baseUrl to outDir but also I have to change js extensions to ts extensions. |
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 |
I highly recommend module-alias |
@bisonfoutu @locinus @psabeckis Hi, someone can help me with this, can not get it to work. |
Any updates on this one. Still facing issue |
Even that module doesnt work as promised |
@kamilmysliwiec how about i update the starter project with paths included and send a PR |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
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
@foo
):app.module.ts
:import { AppController } from '@foo/app.controller';
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
The text was updated successfully, but these errors were encountered: