114

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?

Community
  • 1
  • 1
lenny
  • 2,978
  • 4
  • 23
  • 39
  • Did you install this https://github.com/TypeStrong/ts-node ? – kawadhiya21 Dec 01 '16 at 12:36
  • I've created a new project, installed Mocha and ts-node, and used your command but did not get the error you report having. Everything worked fine from the get go. There's something you did wrong that you are not showing in your question. I would also say that the 18 upvotes on the answer from which you got this method and the lack of downvotes and comments bringing up the error you got is evidence that *it just works*. – Louis Dec 31 '16 at 14:14
  • 40
    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` – Anita Feb 27 '17 at 14:50
  • 2
    I would like this question to be reopened as the answer is not a simple one...Anita's comment is the correct answer – suku Mar 27 '17 at 08:26
  • 1
    Why was the question closed? It's a valid question. – Searene May 24 '17 at 12:03
  • It will work if you do not include the `-g` 'global' flag -- it's a path issue. – Lee Goddard Apr 05 '19 at 07:44

11 Answers11

164

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.

Louie Bertoncin
  • 3,744
  • 2
  • 25
  • 28
  • 14
    You also have to install typescript `npm install typescript --save` –  Jul 09 '18 at 11:58
  • 6
    On my Linux machine I actually needed to install it globally as well `sudo npm install ts-node -g` – Robert Sep 18 '19 at 16:08
  • 3
    Sometimes you don't want to place `ts-node` in package deps and want to use the global one. For such cases linking a local package to the global one using `npm link ts-node` solves the problem. – rcode May 17 '20 at 22:05
  • 3
    I also wanted to confirm what @Lonely said. After `npm install ts-node --save-dev` I then got an error about typescript, so I installed to devDepenancies: `npm install typescript --save-dev` and now I don't get this error. – Joe Sadoski Aug 15 '20 at 07:22
  • Why does this actually work with locally installed ts code? Why not with just global? Could someone explain it to me? I don't get it – augenss Aug 18 '20 at 21:46
  • 2
    i did `npm install ts-node`, `npm install ts-node -g`, `npm install ts-node --save-dev` none works. What other options do I have? – JChao Apr 08 '21 at 17:49
  • `npm install typescript --save-dev` - seem to be required for me – terary Jul 24 '21 at 20:14
  • This answer worked perfectly but i don't get it why it works. Any explanation? – TeachMeJava Nov 06 '22 at 19:30
21

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": []
}

enter image description here

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
  • 4
    If you don't want to move your vscode folder, use; `./myproj/node_modules/ts-node/register` – myol Mar 16 '21 at 20:03
  • In the end if I moved the directory, upon attempting to run my debug config, the test disappeared from my debug dropdown.. so supplying the relative path as @myol has recommended within my config resolved my issue! Thank you both – jimmyNames Feb 18 '23 at 18:27
5

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.

kael
  • 6,585
  • 5
  • 21
  • 27
3

Try this command instead:

mocha --compilers ts:ts-node/register,tsx:ts-node/register

which works for me.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
2

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.

ironchicken
  • 754
  • 6
  • 19
2

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.

lockhrt
  • 597
  • 6
  • 11
0

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"],
    },
  ]
Derek Ziemba
  • 2,467
  • 22
  • 22
0

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

Emon
  • 1,401
  • 14
  • 24
0

I know that I'm late but I had the same problem today You have to give to the --require flag an absolute path. I had the same error and I solved It by going from this:

"script":{
 .....
 "test":"env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --require /node_modules/ts-node/register 'test/**/*.ts'"
 }

To this with a relative path for the flag:

"script":{
 .....
 "test":"env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --require ./node_modules/ts-node/register 'test/**/*.ts'"
 }

The key thing is the dot in the ./node_modules. Of course you have to have your module installed locally or globally installed and linked to the node project

colo
  • 101
  • 8
0

If you don't want to use the local version of ts-node (the one being used by your project), and instead want to use the global version of ts-node you installed, you can do this...

which ts-node # to find the path for the global ts-node
{
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "request": "launch",
      "name": "ts-node",
      "type": "node",
      "program": "THE_PATH_YOU_GOT_FROM_THE_WHICH_CMD",
      "args": [
        "${relativeFile}"
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart"
    }  
  ]
}

You don't need to use the node -r runtime argument if you're using your global installation of ts-node.

cbz
  • 1
  • 1
-5
Error:
module.js:328
    throw err; 
Error: Cannot find module 'ts-node'

Solution: Following command solves the issue.

<b>npm install ts-node --save-dev</b>

(Installs ts-node as a development dependency for your project)

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
user2712873
  • 174
  • 1
  • 2