node.js 找不到模块“ts-node/register”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40910864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find module 'ts-node/register'
提问by lenny
I want to use mocha to test my TypeScript/Angular2 project. I tried to use ts-nodeas described here:
我想用 mocha 来测试我的 TypeScript/Angular2 项目。我试图用TS-节点描述在这里:
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?
我在这里缺少什么?
回答by Louie Bertoncin
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
我也有这个问题。不知道为什么这个 Q 已被关闭。但是在本地安装 ts-node 可以解决这个问题。
npm install ts-node --save-dev
Thanks @Anita, as this was the answer that worked for me too.
谢谢@Anita,因为这也是对我有用的答案。
回答by Sibeesh Venu
Wow, a silly mistake can cost you time. I was facing the same issue when I was trying to debug my nodejsapplication. The mistake I had done was that I have created my .vscodefolder outside of my nodejsapp folder(the directory which had node_modulesin it). When I moved my .vscodeto that folder, everything work fine. Below is my launch.jsonfile.
哇,一个愚蠢的错误会花费你时间。当我尝试调试我的nodejs应用程序时,我遇到了同样的问题。我犯的错误是我在我.vscode的nodejs应用程序文件夹(其中包含的目录node_modules)之外创建了我的文件夹。当我移动.vscode到那个文件夹时,一切正常。下面是我的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": []
}
回答by kael
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-nodeglobally 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:
我ts-node使用sudo npm i -g ts-node. 为了使用 mocha 进行这项工作,我只需要为 mocha 提供模块的绝对路径,如下所示:
mocha -r /usr/lib/node_modules/ts-node/register test/*Test.ts
Hope that helps someone else.
希望能帮助别人。
回答by Mike Lischke
Try this command instead:
试试这个命令:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
which works for me.
这对我有用。
回答by ironchicken
I have mochaand ts-nodeinstalled as dev dependencies in my package. I'm also using pnpm. I originally had a scriptfor testthat was defined as:
我在我的包中作为开发依赖项安装mocha并ts-node安装。我也在使用 pnpm。我原本有一个script针对test的是被定义为:
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 -rpath explicit:
当这停止工作时出现与此问题中报告的相同的错误时,我通过-r明确路径来修复它:
pnpx mocha -r ./node_modules/ts-node/register '*.test.ts'
I'm still confused as to why the original version stopped working.
我仍然对原始版本停止工作的原因感到困惑。
回答by user2712873
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)
(安装 ts-node 作为您项目的开发依赖项)


