Ts-node: Cannot find module 'ts-node/register' in VS Code Debug

Created on 5 Apr 2018  路  6Comments  路  Source: TypeStrong/ts-node

The VS Code Node debugger launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "runtimeArgs": ["-r", "ts-node/register"],
      "args": ["${workspaceFolder}/index.ts"]
    }
  ]
}

I had globally installed the typescript and ts-node

ts-node -v

ts-node v5.0.1
node v9.10.1
typescript v2.8.1

When I execute the debugger, it threw the error:

node -r ts-node/register /home/jancat/projects/Node/huaban-crawl/index.ts 
module.js:545
    throw err;
    ^
Error: Cannot find module 'ts-node/register'
    at Function.Module._resolveFilename (module.js:543:15)
    at Function.Module._load (module.js:470:25)
    at Module.require (module.js:593:17)
    at Function.Module._preloadModules (module.js:750:12)
    at preloadModules (bootstrap_node.js:532:38)
    at startup (bootstrap_node.js:191:9)
    at bootstrap_node.js:666:3
documentation

Most helpful comment

It is also important if your launch.json is not in the same folder as your project (i.e. package.json and therefore node_modules) that you set properly cwd in launch.json, for example:

"cwd": "${workspaceFolder}/<path_to_project>"

as otherwise, VSC will not find ts-node or typescript packages

All 6 comments

You need to npm install ts-node locally, not globally. This may need a PR to documentation if that's unclear.

I have ts-node installed locally. Here's my package.json:

{
  "name": "api",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "ts-node src/index.ts",
    "test": "tslint --project tsconfig.json"
  },
  "devDependencies": {
    "ts-node": "^7.0.1",
    "tslint": "^5.11.0",
    "typescript": "^3.1.3"
  },
  "dependencies": {
    "graphql-yoga": "^1.16.7",
    "prisma-client-lib": "^1.19.0"
  }
}

but I'm still seeing

/usr/local/bin/node -r ts-node/register --inspect-brk=40133 api/src/index.ts 
Debugger listening on ws://127.0.0.1:40133/ceca7723-bfa1-4461-b97a-a17c1a1ec2fc
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
internal/modules/cjs/loader.js:583
    throw err;
    ^
Error: Cannot find module 'ts-node/register'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at Module._preloadModules (internal/modules/cjs/loader.js:805:12)
    at preloadModules (internal/bootstrap/node.js:588:7)
    at startup (internal/bootstrap/node.js:269:9)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
Waiting for the debugger to disconnect...
Error: Cannot find module 'ts-node/register'
loader.js:581
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at Module._preloadModules (internal/modules/cjs/loader.js:805:12)
    at preloadModules (internal/bootstrap/node.js:588:7)
    at startup (internal/bootstrap/node.js:269:9)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)

@samuela And npm start works? And the node_modules is in the directory you're working in and not some child directory like api? This is pure node.js resolution here, not related to ts-node since the file is released with the package on NPM.

@blakeembrey Ah, my node_modules is in api. That's probably what's going on here.

It is also important if your launch.json is not in the same folder as your project (i.e. package.json and therefore node_modules) that you set properly cwd in launch.json, for example:

"cwd": "${workspaceFolder}/<path_to_project>"

as otherwise, VSC will not find ts-node or typescript packages

I have install ts-node globally instead of making package dependency. I tried to link local package to the global one and that was helpful to resolve the ts-node/register issue.

$ npm link ts-node
C:\dev\projects\my-project\node_modules\ts-node -> C:\Program Files\nodejs\node_modules\ts-node

Hope this help!

Was this page helpful?
5 / 5 - 1 ratings

Related issues

huan picture huan  路  3Comments

motss picture motss  路  4Comments

watzon picture watzon  路  3Comments

joshua-tj picture joshua-tj  路  3Comments

cevek picture cevek  路  4Comments