ts-node cannot find module
See original GitHub issueI have two files:
src/app/index.tssrc/config.ts
Within index.ts:
import { getConfig } from "config";
getConfig();
Within config.ts:
export const getConfig = () => console.log("hello world!");
I have a tsconfig.json file:
{
"compilerOptions": {
"outDir": "./ts-build/",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": "src",
"target": "es5"
}
}
Running ts-node ./src/app/index.ts throws me: Error: Cannot find module 'config'. Setting moduleResolution to classic doesn’t change anything.
Versions:
"ts-node": "^3.3.0",
"typescript": "^2.5.2"
Any ideas if this is a ts-node issue, typescript issue or did I do something wrong?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:26 (6 by maintainers)
Top Results From Across the Web
ts-node cannot find module typescript - Stack Overflow
Finally, I got ts-node to work using npx: Install typescript globally: npm i typescript -g. Go to your project directory and link typescript ......
Read more >Cannot find module 'ts-node/register' Error in TypeScript
To solve the error "Cannot find module 'ts-node/register'", install ts-node and typescript as development dependencies by running npm install --save-dev ...
Read more >cannot find module [Node npm Error Solved] - freeCodeCamp
How to Fix the "cannot find module" Error · delete the node modules folder by running rm -rf node_modules · delete package.lock.json file...
Read more >ts-node - npm
Tip: Installing modules locally allows you to control and share the versions through package.json . ts-node will always resolve the compiler ...
Read more >Documentation - Module Resolution - TypeScript
Finally, if the compiler could not resolve the module, it will log an error. ... the TS compiler will follow, it is important...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for the reply @blakeembrey. I’ve run this with
tsc --traceResolutionand the resolution works fine as seen here:What I actually discovered was that I have to have the environment variable
NODE_PATHset to match the baseUrl also. I ended up running the following:NODE_PATH=./src ts-node ./src/app/index.ts.Now I know we can’t set environment variables in tsconfig, this would be convenient and isn’t a ts-node concern. However, do you have any thoughts if environment variables could be somehow settable on the ts-node side? I’m running nodemon in development and there it’s easy to set within
nodemon.json.I also ran into this issue in my NestJS project when I tried enabling debugging in VSCode. I managed to solve this by changing my nodemon.json to:
It was the part
-r tsconfig-paths/registerthat fixed it. This parameter will convert paths into physical file paths.