tsconfig-paths: tsconfig-paths doesn't work with node (works with ts-node)
$ node -r tsconfig-paths/register dist/index.js
module.js:550
throw err;
^
Error: Cannot find module '@modules/webhooks'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._resolveFilename (/home/niko/WebstormProjects/guild-review/node_modules/tsconfig-paths/lib/register.js:73:40)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/niko/WebstormProjects/guild-review/server/dist/index.js:5:20)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
Repro: https://github.com/darkbasic/guild-review
yarn && yarn workspace server build && cd server && node -r tsconfig-paths/register dist/index.js
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 26
- Comments: 33
Commits related to this issue
- Fix missing ts-node and tsconfig-paths deps during runtime For more info, see: https://github.com/dividab/tsconfig-paths/issues/61 — committed to wolfpilot/demotivator-rest-api by wolfpilot 3 years ago
- Fix missing ts-node and tsconfig-paths deps during runtime For more info, see: https://github.com/dividab/tsconfig-paths/issues/61 — committed to wolfpilot/demotivator-rest-api by wolfpilot 3 years ago
As a workaround, you can do
node -r ts-node/register -r tsconfig-paths/register dist/index.js
. @kel-sakal-biyik @darkbasicMay I ask for an update on this issue?
So I end up using the following:
"start": "node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/main.js"
Hope it doesn’t have any negative impact.
I managed to make it work by using the
TS_NODE_PROJECT
env variable to point to a tweaked tsconfig file. In practice this means:Launch script
tsconfig.prod.json
For some reason I could not override the
compilerOptions.paths
to rewrite them from the newbaseUrl
, but this particular setup seems OK.Right, but the particularity about this temporary workaround is that I need to move
ts-node
todependencies
section. For example to deploy my app into a Docker container.Ideal solution (again) would be just run the transpiled code:
I ended up going with a modified solution based on @mkalam-alami’s approach. The issue is that the
tsconfig.json
at the top level sets abaseUrl
that makes sense for the top level but not the compiled js in yourdist/
orbuild/
. My work around was just to copy the top leveltsconfig.json
into thetsc
output dir and use that for theTS_NODE_PROJECT
variable. That way you preserve your defaults without any custom files, and because you moved the file to that directory, you change the relative position of yourbaseUrl
so that it again makes sense to the rest of the files you’re executing. Example package.json below contrasting dev vs prod runsI have created a persistent Typescript paths replacer for those wants to replace TS path aliases directly. No runtime replace.
So the issue is, that the
baseUrl
will be resolved relative totsconfig.json
in root of your project, but in reality, it should point to yourdist
folder (or wherever your compiled files are). This package is working fine and it’s not actually a bug, but I think adding something likeTS_PATHS_ROOT
environment variable, that would allow people to override the root ofbaseUrl
would be much appreciated (actually there’s already a PR for that - https://github.com/dividab/tsconfig-paths/pull/114)So the node.js is trying to load your actual typescript source files. Using the
-r ts-node/register/transpile-only
workaround basically means you will compile your typescript files twice because you’ll be importing the typescript files (that use the paths) not the compiled javascript. It’s almost the same as running ts-node on your uncompiled index file.One possible solution is to copy
tsconfig.json
to yourdist
path and set the current working directory (CWD) to said dist path when running the file. You have to set the CWD todist
because thetsconfig.json
in CWD has the highest priority.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
dist
folder.Example
Project structure
then you can use vscode to debug run with following config:
@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.
@TSiege Your solution is the more elegant solution while PR #114 is approved. It’s works for me. This is my full config…
tsconfig.json
Scripts on package.json. It’s going to build then run build
Windows user, remember using copy instead of cp command with
\\
and not/
Thanks, @TSiege. Your solution is awesome. FYI. Considering the tsconfig.json at the
paths
section. If you define those absolute paths be like thisyou should set the
outDir
to be like “./dist/src” as well.+1
So no solution for this yet basically, only workarounds for now ?
compile
again when runningstart
after already doing it when buildingAll above may be not elegant or convenient, I won’t use these solutions because ROI is too low
Solution above didn’t work after deployment (probably more variables involved in building Docker image and environment variables etc.) So tried with similar to https://github.com/dividab/tsconfig-paths/issues/61#issuecomment-513642851 to actually have proper imports in the resulting *.js in “/dist” folder. But eventually ended with “module-alias” https://www.npmjs.com/package/module-alias as it is easy to use and just works.
So this bit from the README should be removed to avoid confusion