Ts-node: Cannot find module 'ts-node/register' in global

Created on 27 Nov 2018  路  4Comments  路  Source: TypeStrong/ts-node

It work on locally install, but not work on globally install

npm install ts-node -g
npm install typescript -g
package.json
{
  ...
  scripts: {
    ....
    "test": "mocha --require ts-node/register test/*.ts
  }
}

when run npm run test, it throwing:

   Error: Cannot find module 'ts-node/register'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)                                                  
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)                                                             
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at requires.forEach.mod (/home/s97712/extdata/sqlbind.js/node_modules/mocha/bin/_mocha:511:3)                                
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/s97712/extdata/sqlbind.js/node_modules/mocha/bin/_mocha:510:10)                                 
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)                                                     
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)                                                              
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)                                                           
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

All 4 comments

This isn鈥檛 an issue with ts-node. Mocha can鈥檛 resolve a global module. Why don鈥檛 you install locally? Otherwise you can probably work around it by also using a global mocha, but it depends how it resolves the require.

I'm also getting this after following these instructions

https://medium.com/@dupski/debug-typescript-in-vs-code-without-compiling-using-ts-node-9d1f4f9a94a

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "src/index.ts",
      "type": "node",
      "request": "launch",
      "args": ["${workspaceFolder}/src/index.ts"],
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}",
      "protocol": "inspector"
    }
  ]
}
  },
  "devDependencies": {
    "@types/jest": "^23.3.13",
    "@types/node": "^10.12.20",
    "jest": "^24.0.0",
    "serve": "^10.1.1",
    "ts-jest": "^23.10.5",
    "ts-node": "^8.0.2",
    "typescript": "^3.2.4"
  }
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:812:12)
    at preloadModules (internal/bootstrap/node.js:599:7)
    at startup (internal/bootstrap/node.js:273:9)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

@lukepighetti Probably an obvious question, but did you do an npm install? That's a normal node.js error with being unable to resolve the module from your working directory. E.g., if you do ls node_modules/ts-node/register do you see an index.js file?

I do, what's fascinating is that if I run node on the command line with register it works. It only fails when the debugger tries to run it. Like I can copy-paste the debugger command from console into my terminal and it works.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sodiumjoe picture sodiumjoe  路  4Comments

motss picture motss  路  4Comments

huan picture huan  路  3Comments

conordickinson picture conordickinson  路  4Comments

remojansen picture remojansen  路  4Comments