2

当我通过 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)

我的测试使用以下 mocha.opts 和 package.json 脚本在 VSCode 之外运行良好。

//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 -Dyarn add typescript -D. 我的 node_modules/ 目录中有 ts-node 。

我的 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);

4

0 回答 0