Cannot find module 'ts-node/register'

前端 未结 7 1679
青春惊慌失措
青春惊慌失措 2021-02-06 20:01

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
<
相关标签:
7条回答
  • 2021-02-06 20:42

    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.

    0 讨论(0)
  • 2021-02-06 20:46

    Try this command instead:

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

    which works for me.

    0 讨论(0)
  • 2021-02-06 20:46
    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)

    0 讨论(0)
  • 2021-02-06 20:54

    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.

    0 讨论(0)
  • 2021-02-06 21:02

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

    0 讨论(0)
  • 2021-02-06 21:02

    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"],
        },
      ]
    
    0 讨论(0)
提交回复
热议问题