简体   繁体   English

找不到模块“ts-node/register”

[英]Cannot find module 'ts-node/register'

I want to use mocha to test my TypeScript/Angular2 project.我想使用 mocha 来测试我的 TypeScript/Angular2 项目。 I tried to use ts-node as described here :我尝试按照此处所述使用ts-node

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'找不到模块“ts-node/register”

What am I missing here?我在这里错过了什么?

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. 但是在本地安装ts-node可以解决此问题。 npm install ts-node --save-dev

Thanks @Anita, as this was the answer that worked for me too. 感谢@Anita,因为这也是对我有用的答案。

Wow, a silly mistake can cost you time. 哇,愚蠢的错误会浪费您的时间。 I was facing the same issue when I was trying to debug my nodejs application. 当我尝试调试nodejs应用程序时,我遇到了同样的问题。 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). 我犯的错误是我在nodejs应用程序文件夹(其中node_modules的目录)之外创建了.vscode文件夹。 When I moved my .vscode to that folder, everything work fine. 当我将.vscode移至该文件夹时,一切正常。 Below is my launch.json file. 以下是我的launch.json文件。

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

在此处输入图片说明

Try this command instead: 请尝试以下命令:

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

which works for me. 这对我有用。

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 . 我使用sudo npm i -g ts-node全局安装了ts-node 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能够正常工作,我只需要给mocha到模块的绝对路径,就像这样:

mocha -r /usr/lib/node_modules/ts-node/register test/*Test.ts

Hope that helps someone else. 希望对别人有帮助。

Error: module.js:328 throw err; Error: Cannot find module 'ts-node'

Solution: Following command solves the issue. 解决方案:使用以下命令可以解决此问题。
npm install ts-node --save-dev npm install ts-node --save-dev

(Installs ts-node as a development dependency for your project) (将ts-node安装为项目的开发依赖项)

I have mocha and ts-node installed as dev dependencies in my package. 我在程序包中安装了mochats-node作为dev依赖项。 I'm also using pnpm. 我也在用pnpm。 I originally had a script for test that was defined as: 我最初有一个test script ,定义为:

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: 当此命令停止工作并出现与该问题所报告的错误相同的错误时,我通过显式显示-r路径来解决此问题:

pnpx mocha -r ./node_modules/ts-node/register '*.test.ts'

I'm still confused as to why the original version stopped working. 对于原始版本为何停止工作,我仍然感到困惑。

Assuming you rather not install ts-node locally and would prefer to use the globally installed node_module.假设您不想在本地安装 ts-node,而是更愿意使用全局安装的 node_module。 The following examples show what to do for Windows and assumes you're using NVM.以下示例显示了为 Windows 执行的操作,并假设您使用的是 NVM。 If not using NVM, replace NVM_SYMLINK with C:\Program Files\nodejs如果不使用 NVM,请将NVM_SYMLINK替换为C:\Program Files\nodejs

Example for command line:命令行示例:

node -r "%NVM_SYMLINK%\node_modules\ts-node\register" script.ts arg1 arg2

Example for bash: bash 示例:

node -r "$NVM_SYMLINK/node_modules/ts-node/register" script.ts arg1 arg2

Example for vscode launch.config 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"],
    },
  ]

I have the same issue while using "jasmine-unit-test-generator".我在使用“jasmine-unit-test-generator”时遇到了同样的问题。 I fix it by runing npm link ts-node .我通过运行npm link ts-node来修复它。 you can refer to https://github.com/TypeStrong/ts-node/issues/565可以参考https://github.com/TypeStrong/ts-node/issues/565

Use /node_modules/ts-node/register in place of ts-node/register .使用/node_modules/ts-node/register代替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:并确保 ts-node 在您的项目中本地安装,例如:

npm install ts-node @types/node typescript

This solution doesn't need you to install ts-node globally.此解决方案不需要您全局安装 ts-node。 And it works cross platform and for everyone.它适用于跨平台和每个人。

I know that I'm late but I had the same problem today You have to give to the --require flag an absolute path.我知道我迟到了,但我今天遇到了同样的问题你必须给--require标志一个绝对路径。 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 .关键是./node_modules中的点。 Of course you have to have your module installed locally or globally installed and linked to the node project当然,您必须在本地或全局安装模块并链接到节点项目

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...如果您不想使用本地版本的 ts-node(您的项目正在使用的那个),而是想使用您安装的全球版本的 ts-node,您可以这样做......

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.如果您使用的是 ts-node 的全局安装,则不需要使用 node -r 运行时参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ts-node 转译 mocha 测试时,我找不到模块“tsconfig/register” - when transpiling mocha test using ts-node i cannot find module 'tsconfig/register' 量角器黄瓜-'未处理的拒绝错误:找不到模块'ts-node / register' - Protractor Cucumber - 'Unhandled rejection Error: Cannot find module 'ts-node/register' mikro-orm CLI:找不到模块“ts-node” - mikro-orm CLI: Cannot find module 'ts-node' 带有require的'ts节点编程用法'('ts-node / register') - ts-node programmatic usage with require('ts-node/register') 无法再让节点或 ts-node/register 使用 es2020(flatMap 不是函数) - Cannot get node or ts-node/register to use es2020 anymore (flatMap is not a function) 要求ts-node和ts-node / register有什么区别 - What's the difference between requiring ts-node and ts-node/register 使用 ts-node 编译 nodejs 代码时出现问题(不能在模块外使用 import 语句) - Issues compiling nodejs code with ts-node (Cannot use import statement outside a module) 使用 ts-node 执行 typescript 代码会出现“无法在模块外使用导入语句”错误 - Executing typescript code using ts-node gives “Cannot use import statement outside a module” error ts-node 无法导入类型声明文件 - ts-node cannot import type declaration files ts-node 找不到我的类型定义文件 - ts-node can't find my type definition files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM