tsconfig-paths: Cannot Find Module
It seems my module paths do not get resolved correctly when using node
or ts-node
src/index.ts
import cleanup from 'node-cleanup';
import ArimaClient from '@client/ArimaClient';
// ...
tsconfig.json
{
"compilerOptions": {
"target": "ES2019",
"lib": ["ES2019"],
"moduleResolution": "node",
"module": "commonjs",
"strict": true,
"alwaysStrict": true,
"useDefineForClassFields": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"sourceMap": true,
"inlineSources": true,
"esModuleInterop": true,
"removeComments": true,
"skipLibCheck": true,
"baseUrl": ".",
"outDir": "dist/",
"typeRoots": ["node_modules/@types", "src/typings"],
"paths": {
"@root/*": ["src/*"],
"@client/*": ["src/lib/client/*"],
"@database/*": ["src/lib/database/*"],
"@structures/*": ["src/lib/structures/*"],
"@structures": ["src/lib/structures"],
"@utils/*": ["src/lib/utils/*"],
"@utils": ["src/lib/utils"]
}
},
"include": ["src"]
}
Scripts (I get the same error for both)
ts-node --files src/index.ts -r dotenv/config -r tsconfig-paths/register NODE_ENV=dev
tsc && node -r dotenv/config -r tsconfig-paths/register .
Error
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module '@client/ArimaClient'
Require stack:
- C:\Users\me\OneDrive\Desktop\Arima\dist\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._resolveFilename (C:\Users\me\OneDrive\Desktop\Arima\node_modules\tsconfig-paths\lib\register.js:75:40)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\me\OneDrive\Desktop\Arima\dist\index.js:7:39)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\me\\OneDrive\\Desktop\\Arima\\dist\\index.js' ]
}
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 33
- Comments: 21
It looks like you are having the same problem that I’m currently having with this. What seems to be happening is that
tsconfig-paths
is looking attsconfig.json
’sbaseUrl
but not takingoutDir
into consideration. I found this by manually editing theregister
function innode_modules/tsconfig-paths/lib/register.js
so that it would log some debug info when trying to resolve my path mappings like this:register.js
The output from here was like this (
build
is what I’ve specified foroutDir
):Failure shown in output
If I change
baseUrl
to matchoutDir
(i.e.build
for my example,dist
for OP) after runningtsc
thentsconfig-paths
gets it right. In my opinion, this is a bug, but it is possible to work around by adapting the “Bootstrapping with explicit params” example in the README.md.What I did to make it work was replace
node -r tsconfig-paths/register ...
withnode -r ./register-paths.js ...
(and createregister-paths.js
as shown below). Basically this tellstsconfig-paths
a differentbaseUrl
that incorporatesoutDir
.`register-paths.js`
well, after suffering a few hours, I got a solution I have used the
ts-node
package, and I got the same errorError: Cannot find module '@modules/logger'
You need to add
ts-node
configuration in thetsconfig.json
file. You can get more infor at ts-node+1 this package is useless I think, time to find a different solution
EDIT: This package seems to work good for my use case https://github.com/justkey007/tsc-alias
Actually I got it to work now using TS_NODE_BASEURL. I have a layout like
and im my tsconfig.json
Now when I put a script in my package.json like so
I can run it with
npm run myscript
just fine with"@/mymodule"
imports.What didn’t work before was when I had
baseUrl
andpaths
switched in tsconfig.json likeThis doesn’t work for me