问题
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?
回答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.
回答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": []
}
回答3:
Try this command instead:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
which works for me.
回答4:
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.
回答5:
Error:
module.js:328
throw err;
Error: Cannot find module 'ts-node'
Solution: Following command solves the issue.
npm install ts-node --save-dev
(Installs ts-node as a development dependency for your project)
回答6:
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.
来源:https://stackoverflow.com/questions/40910864/cannot-find-module-ts-node-register