Repository navigation
ts-node-dev Cannot find module #953
Description
Suppose I have this kind of structure:
src
├── app.ts
├── config.ts
├── handlers
│ └── item.ts
└── types.ts
app.ts imports item.ts like so:
import itemHandler from './handlers/item';item.ts on its turn imports types.ts like so:
import { ItemParams } from '/types';This works absolutely fine. Exactly like I expect it. After all types.ts is at the root, and that's where I'm importing it from. So, I'll add another import:
import { ItemParams } from '/types';
import config from '/config';Boom. Error: Cannot find module '/config'
That one cannot be found. How come? It's imported from the same file, from the same location, using the same absolute notation. How is it that it can find the one, but not the other?
I've checked to see if it matters what they export - it doesn't, as I suspected.
If nothing else, I'd like to know where it cannot find the module requested. As in, where did it look for it? But really this should just work, especially since running tsc works.
The only workaround I've found is to turn my imports relative:
import { ItemParams } from '../types';
import config from '../config';But this is undesirable. What if I move the file with imports to another location? And also this is not as easily reusable.
For reference, here's my tsconfig.json:
{
"include": ["src"],
"compilerOptions": {
"target": "ES2019",
"module": "CommonJS",
"lib": ["dom", "esnext", "es2017"],
"importHelpers": true,
"declaration": true,
"downlevelIteration": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"outDir": "./dist",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}Versions:
Node.js: 12.14.1
NPM: 6.13.7
Typescript: 3.7.5
ts-node-dev: 1.0.0-pre.44
Activity
[-]Cannot find module[/-][+]ts-noce-dev Cannot find module[/+][-]ts-noce-dev Cannot find module[/-][+]ts-node-dev Cannot find module[/+]cspotcode commentedon Feb 3, 2020
@thany
I see you're using
"paths"in your tsconfig.json. Are you also usingtsconfig-paths?https://github.com/TypeStrong/ts-node#loading-tsconfigjson
You say that
tscsucceeds. Are you able to run the code that has been emitted bytsc? Or do you get the same error when you try to run it?3rd commentedon Feb 4, 2020
Running into the same issue, @thany are you using yarn 2 (berry) by any chance?
thany commentedon Feb 6, 2020
@cspotcode I'll have to verify that when I get home. I think I remember seeing it running outside of ts-node-dev, but I'm not 100% sure. I'm not aware of having installed/configured
tsconfig-paths, so I'll check that as well.@3rd I'm using NPM, as you can see in my opening comment. Besides, I honestly can't imagine the package manager causing this issue.
cspotcode commentedon Feb 6, 2020
@thany Our README has a section that explains why you need to use tsconfig-paths in certain situations.
https://github.com/TypeStrong/ts-node#loading-tsconfigjson
thany commentedon Feb 6, 2020
I noticed the stuff emitted from
tscdoesn't work either and I couldn'tmake it work in any way other than by switching to webpack. Doing so reveals that both the aforementioned modules cannot be found, and I cannot figure out for the life of me how to tell the compiler/loader/config where to look...cspotcode commentedon Feb 6, 2020
Have you tried my suggestions? Have you looked at the README section that I pointed you to?
thany commentedon Feb 6, 2020
I have, but I don't really see it explaining why I need that package... Shouldn't it just, you know, work?
cspotcode commentedon Feb 6, 2020
No, it doesn't really work that way. This is part of TypeScript's design, so it's not really a ts-node behavior.
This section of the TypeScript handbook explains it:
https://www.typescriptlang.org/docs/handbook/module-resolution.html#additional-module-resolution-flags
The tsconfig-paths README explains a bit more:
https://www.npmjs.com/package/tsconfig-paths
It also has some examples of usage with ts-node, which should set you on the right track.
I'm going to close this since it's not a ts-node issue. The linked resources above should be everything you need to get your code working. Good luck!
VadimZP commentedon Dec 26, 2023
For anyone who is trying to find a solution - https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428
thany commentedon Dec 27, 2023
Thanks, @VadimZP, but honestly I don't agree with the whole paradigm of throwing more stuff towards my project, and then hope that doesn't also break at some point, even if it works today. Why can't things "just work"?