当我通过VSCode调试器运行测试时,我突然收到以下错误,即mocha无法找到ts-node / register模块。这似乎是在使用nvm从v8.6更新到Node v9之后才开始发生的。在Vscode之外运行时,测试运行良好。
Exception has occurred: Error
Error: ENOENT: no such file or directory, access 'ts-node/register'
at Object.fs.accessSync (fs.js:322:11)
at fs.existsSync (fs.js:343:8)
at Command.program.on.mod (/${project_path}/node_modules/mocha/bin/_mocha:267:15)
at Command.emit (events.js:185:15)
at Command.parseOptions (/${project_path}/node_modules/mocha/node_modules/commander/index.js:710:14)
at Command.parse (/${project_path}/node_modules/mocha/node_modules/commander/index.js:454:21)
at Object.<anonymous> (/${project_path}/node_modules/mocha/bin/_mocha:281:9)
at Module._compile (internal/modules/cjs/loader.js:651:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
我的测试在VSCode之外使用以下mocha.opts和package.json脚本运行良好。
//mocha.opts
-r ts-node/register
-r source-map-support/register
-u tdd
--timeout 5000
--colors
build/test/**/*Test.js
//package.json
"scripts": {
"pretest": "yarn run build",
"test": "mocha",
自从使用NVM更新到Node v9后,我还通过brew install yarn --without-node
以及yarn add ts-node -D
和yarn add typescript -D
重新安装了纱线。我的node_modules /目录中有ts节点。
我的VSCode launch.json看起来像:
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceFolder}/",
"internalConsoleOptions": "openOnSessionStart"
},
当我通过调试器运行测试时,vscode会立即在以下行中断,并显示上面显示的异常:
binding.access(pathModule.toNamespacedPath(path), mode);