Open mdbr000-np opened 1 year ago
This package doesn't affect TypeScript compilation, and therefore the module paths are left as-is in the final compiled JavaScript. If you need to convert these (e.g. are unable to use the registration described below), you need to use a different solution.
However, you can register the paths in runtime via this package by altering the scripts
in your package.json
that run the node process to include the flag -r tsconfig-paths/register
. For example, changing your boot
-script to be node -r tsconfig-paths/register dist/src/index.js
should resolve your issue.
For some reason, even registering tsconfig-paths/register
while calling the script via node is not working for me (still throws error about not finding the "pathed" modules). However, running via ts-node
does work as expected (via registration in tsconfig.json
).
"outDir": "./dist",
"baseUrl": "./src",
"paths": {
"@services/*": ["./services/*"],
},
"ts-node": {
"require": ["tsconfig-paths/register"]
}
> node -r tsconfig-paths/register dist/src/index.js
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '@services/randomService'
Require stack:
- /<PATH>/dist/src/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename (/<PATH>/node_modules/tsconfig-paths/lib/register.js:103:40)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/<PATH>/dist/src/index.js:4:27)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/<PATH>/dist/src/index.js'
]
}
Try setting TS_NODE_BASEURL=./dist env when running node...
For some reason, even registering
tsconfig-paths/register
while calling the script via node is not working for me (still throws error about not finding the "pathed" modules). However, running viats-node
does work as expected (via registration intsconfig.json
)."outDir": "./dist", "baseUrl": "./src", "paths": { "@services/*": ["./services/*"], }, "ts-node": { "require": ["tsconfig-paths/register"] }
> node -r tsconfig-paths/register dist/src/index.js node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module '@services/randomService' Require stack: - /<PATH>/dist/src/index.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._resolveFilename (/<PATH>/node_modules/tsconfig-paths/lib/register.js:103:40) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object.<anonymous> (/<PATH>/dist/src/index.js:4:27) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/<PATH>/dist/src/index.js' ] }
@kendallroth That would seem to mean that the plugin is unable to access the tsconfig.json
-file when you're running Node. You should probably copy it into your dist/
folder (incorporate it into your build script) in order for Node to find it when you're launching your application.
Ah, that is an excellent point, and I know that the TS config file was certainly not being copied during the build. However, we eventually decided to go with an approach during build step, simply to avoid having to have any typescript references or packages in the production environment. However, thanks for the answer, and I hope it can help someone else in the future!
@kendallroth What approach did you take? I'm suffering from the same issue and at this point, I'm planning to just use ts-node in production.
I used resolve-tspaths
as part of the build step (ie. "build": "tsc && resolve-tspaths"
).
I used
resolve-tspaths
as part of the build step (ie."build": "tsc && resolve-tspaths"
).
This was the easiest and most straight forward solution for my needs. Thanks @kendallroth !
@kendallroth big thx
i was having this issue and this comment https://github.com/dividab/tsconfig-paths/issues/222#issuecomment-1272791309 resolved in conjunction with TS_NODE_PROJECT
we use: TS_NODE_BASEURL=dist/src/ TS_NODE_PROJECT=tsconfig.json node -r tsconfig-paths/register dist/src/index
No matter what I seem to try, the values defined in
paths
in mytsconfig.json
file are not being transformed bytsconfig-paths
, which results in node being unable to resolves any modules.Giving an example, where I would expect
@graphql-handlers
imports in my ts files to be converted tographql-handlers
in my compiled .js... but when you examine the output in thedist
folder, all of the paths still show@graphql-handlers
which renders node unable to run.tsconfig.json
package.json
index.ts
Run command:
rm -rf dist; npm run compile; node_modules/.bin/ts-node -r tsconfig-paths/register dist/index.js
Error output:
Looking at the compiled output in dist/index.js, @graphql-handlers has not been converted: