question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-node cannot find module

See original GitHub issue

I have two files:

  • src/app/index.ts
  • src/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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:26 (6 by maintainers)

github_iconTop GitHub Comments

64reactions
tedcurrentcommented, Sep 12, 2017

Thanks for the reply @blakeembrey. I’ve run this with tsc --traceResolution and the resolution works fine as seen here:

======== Resolving module 'config' from 'C:/Dev/test-ts-node/src/app/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to 'C:/Dev/test-ts-node/src', using this value to resolve non-relative module name 'config'.
Resolving module name 'config' relative to base url 'C:/Dev/test-ts-node/src' - 'C:/Dev/test-ts-node/src/config'.
Loading module as file / folder, candidate module location 'C:/Dev/test-ts-node/src/config', target file type 'TypeScript'.
File 'C:/Dev/test-ts-node/src/config.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to 'C:/Dev/test-ts-node/src/config.ts'. ========

What I actually discovered was that I have to have the environment variable NODE_PATH set 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.

57reactions
abinicicommented, Mar 9, 2020

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:

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "node -r tsconfig-paths/register -r ts-node/register src/main.ts"
}

It was the part -r tsconfig-paths/register that fixed it. This parameter will convert paths into physical file paths.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found