'Cannot find module 'ts-node/register'
I want to use mocha to test my TypeScript/Angular2 project. I tried to use ts-node as described here:
npm install -g ts-node
but when running
mocha --require ts-node/register -t 10000 ./**/*.unit.ts
I get an error
Cannot find module 'ts-node/register'
What am I missing here?
Solution 1:[1]
Since the answer that works for a lot of people appears to be hidden in the comments, I'll post it as an actual answer to the question, now that it appears the question has been reopened.
I had this problem as well. Not sure why this Q has been closed. but installing ts-node locally fixes this.
npm install ts-node --save-dev
Thanks @Anita, as this was the answer that worked for me too.
Solution 2:[2]
Wow, a silly mistake can cost you time. I was facing the same issue when I was trying to debug my nodejs application. The mistake I had done was that I have created my .vscode folder outside of my nodejs app folder(the directory which had node_modules in it). When I moved my .vscode to that folder, everything work fine. Below is my launch.json file.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "index",
"args": [
"src/index.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register"
],
"cwd": "${workspaceFolder}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
],
"compounds": []
}
Solution 3:[3]
I know this is kind of old, but I ran into this too and wanted to offer the solution I'm currently using.
I installed ts-node globally using sudo npm i -g ts-node. To make this work with mocha, I just had to give mocha the absolute path to the module, like this:
mocha -r /usr/lib/node_modules/ts-node/register test/*Test.ts
Hope that helps someone else.
Solution 4:[4]
Try this command instead:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
which works for me.
Solution 5:[5]
I have mocha and ts-node installed as dev dependencies in my package. I'm also using pnpm. I originally had a script for test that was defined as:
pnpx mocha -r ts-node/register '*.test.ts'
When this stopped working with the same error as reported in this question I fixed it by making the -r path explicit:
pnpx mocha -r ./node_modules/ts-node/register '*.test.ts'
I'm still confused as to why the original version stopped working.
Solution 6:[6]
Assuming you rather not install ts-node locally and would prefer to use the globally installed node_module. The following examples show what to do for Windows and assumes you're using NVM. If not using NVM, replace NVM_SYMLINK with C:\Program Files\nodejs
Example for command line:
node -r "%NVM_SYMLINK%\node_modules\ts-node\register" script.ts arg1 arg2
Example for bash:
node -r "$NVM_SYMLINK/node_modules/ts-node/register" script.ts arg1 arg2
Example for vscode launch.config
"configurations": [
{
"name": "utils/roll_browser",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
/* Slower startup. Runs full typescript validation */
// "${env:NVM_SYMLINK}/node_modules/ts-node/register"
/* Faster startup. Doesn't check types or verify the code is valid */
"${env:NVM_SYMLINK}/node_modules/ts-node/register/transpile-only"
],
"args": ["${workspaceFolder}/utils/roll_browser.ts", "chromium", "756141"],
},
]
Solution 7:[7]
I have the same issue while using "jasmine-unit-test-generator". I fix it by runing npm link ts-node.
you can refer to https://github.com/TypeStrong/ts-node/issues/565
Solution 8:[8]
Use /node_modules/ts-node/register in place of ts-node/register. So in your case it'll become:
mocha --require /node_modules/ts-node/register -t 10000 ./**/*.unit.ts
and make sure ts-node is installed locally in your project like:
npm install ts-node @types/node typescript
This solution doesn't need you to install ts-node globally. And it works cross platform and for everyone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Louie Bertoncin |
| Solution 2 | Sibeesh Venu |
| Solution 3 | kael |
| Solution 4 | Mike Lischke |
| Solution 5 | ironchicken |
| Solution 6 | Derek Ziemba |
| Solution 7 | Emon |
| Solution 8 | lockhrt |

