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 doesn't work with node (works with ts-node) #61
Comments
I have the same problem. I also tried to put tsconfig.json file under dist but didn't help. |
As a workaround, you can do |
@darkbasic I moved a tsconfig.json file under dist folder with only path configuration and run the index.js under dist folder. By doing so it read the tsconfig file under the dist folder and it worked. You can give it a try, it might help you too. |
Yep just tried it not working with node, and @dotansimha solution I believe it's not optimal for production environment. |
@bushybuffalo reported a fix here. I couldn't get it to work with my project but our configuration is more complicated than the example. I went with @kel-sakal-biyik's solution for now. |
So I end up using the following:
Hope it doesn't have any negative impact. |
Right, but the particularity about this temporary workaround is that I need to move Ideal solution (again) would be just run the transpiled code: {
"prod": "node -r tsconfig-paths/register dist/main.js"
} |
May I ask for an update on this issue? |
+1 |
I have created a persistent Typescript paths replacer for those wants to replace TS path aliases directly. No runtime replace. |
@chanlito Tried your solution, but I'm having the following error message.
Any ideas? |
@jeffminsungkim are you using |
So this bit from the README should be removed to avoid confusion |
@chanlito I believe that the Nest.js framework uses |
I managed to make it work by using the Launch script
tsconfig.prod.json {
"extends": "./tsconfig.js",
"compilerOptions": {
"baseUrl": "dist"
}
} For some reason I could not override the |
Running into this issue as well. Seems like the common solution requires changing the |
So the issue is, that the So the node.js is trying to load your actual typescript source files. Using the One possible solution is to copy Another possible solution (and probably much cleaner) is to use mentioned https://github.com/ilearnio/module-alias, just keep in mind you have to point to your ExampleProject structure
then you can use vscode to debug run with following config:
|
@ejhayes sorry, but can you explain what you mean by that? I'm usually specifying '.' as the baseUrl and then I get the explained error. Right now, I'm sticking with the workaround from above (#61 (comment)) |
For me all of the above didn't work. Also had to use json5, cause have comments in tsconfig.json. my "./tsconfig" "outDir": "./dist", "./tsconfig-paths-bootstrap.js" (also in the root of the project) const json5 = require("json5");
const fs = require("fs");
const tsConfigPaths = require("tsconfig-paths");
const tsConfigPath = "./tsconfig.json";
const data = fs.readFileSync(tsConfigPath);
const tsConfig = json5.parse(data);
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),
),
}),
{},
),
}); |
Solution above didn't work after deployment (probably more variables involved in building Docker image and environment variables etc.) |
I ended up going with a modified solution based on @mkalam-alami's approach. The issue is that the
|
Thanks, @tsiege. Your solution is awesome. "paths": {
"@/*": ["./src/*"]
} you should set the |
Instead of
use:
inspiration from: https://youtu.be/QVxxgEyZt9Y (TDD With TypeScript, Express, NodeJS, and Mocha Unit Tests) |
@tsiege Your solution is the more elegant solution while PR #114 is approved. tsconfig.json{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@server/*": ["./src/server/*"],
"@globals/*": ["./src/globals/*"],
"@plugins/*": ["./src/plugins/*"],
"@modules/*": ["./src/modules/*"]
},
"esModuleInterop": true,
"resolveJsonModule": true,
},
"exclude": ["node_modules", "dist", "tests"],
"ts-node": {
"require": ["tsconfig-paths/register"]
}
} Scripts on package.json. It's going to build then run buildWindows user, remember using copy instead of cp command with
|
All above may be not elegant or convenient, I won't use these solutions because ROI is too low |
This solved my problem |
You can now specify
|
After some confusion with a lot of approaches to fix. I was able to fix the issue just using the env var suggested in the documentation. I hope that will help others with the same problem. I believe this is not an issue anymore. My tsconfig.json: {
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es6",
"moduleResolution": "Node",
"module": "CommonJS",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "./src",
"paths": {
"@config/*": ["./config/*"],
"@controllers/*": ["./controllers/*"],
"@models/*": ["./models/*"],
"@repositories/*": ["./repositories/*"],
"@services/*": ["./services/*"],
"@tests/*": ["../tests/*"]
}
},
"include": ["./src/**/*"]
} My script to start the server: "start": "tsc && TS_NODE_BASEURL=./built node -r tsconfig-paths/register ./built/app.js", Current versions are:
In case someone wanna confirm, that's the source code: https://github.com/rogeraraujo90/hey-freela |
So no solution for this yet basically, only workarounds for now ?
|
@rogeraraujo90 your solution works only at runtime but the transpiled javascript does not have the resolved paths |
I'm not working on that project currently. By I'm using a different library in a new project and it's working. Take a look at https://github.com/benyap/resolve-tspaths |
tsconfig.json
|
Repro: https://github.com/darkbasic/guild-review
yarn && yarn workspace server build && cd server && node -r tsconfig-paths/register dist/index.js
The text was updated successfully, but these errors were encountered: