[ ] 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.
When using Typescript paths in tsconfig, the project can be built with nodemon, but not with tsc (with npm run start:prod
).
I would expect the same code to work regardless its being built for development or production.
@foo
):"paths": {
"@foo/*": [
"./*"
]
}
app.module.ts
:import { AppController } from '@foo/app.controller';
npm run start:prod
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.
Nest version: 5.1.0
For Tooling issues:
- Node version: 10.8.0
- Platform: Windows
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
@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.
tsconfig-paths-bootstrap.js
to the root of the project, and paste the code from the docs in it.start:prod
script to: node -r ./tsconfig-paths-bootstrap.js dist/main.js
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 ?
Thanks for sharing your solution @bushybuffalo. If you have some time, you can create a PR to the starter project :)
@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...
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`;
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
When building for development, node is looking for ts
files in src
folder.
When building for production, node should be looking for js
files in dist
folder. But it is actually looking for ts
files because I have an "unexpected token {" error style.
That is why I have to change baseUrl to outDir but also I have to change js extensions to ts extensions.
I think we don't have the same issue.
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 @/**/*
.
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
I highly recommend module-alias
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.
Most helpful comment
@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 withtsc
.EDIT: I managed to get a working solution by following theses instructions from
tsconfig-paths
docs. I created an example repo here.tsconfig-paths-bootstrap.js
to the root of the project, and paste the code from the docs in it.start:prod
script to:node -r ./tsconfig-paths-bootstrap.js dist/main.js
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 ?