mocha 运行 NPM 测试但不是常规的 mocha CLI 命令

mocha running with NPM test but not regular mocha CLI command

我试图了解在这种情况下我做错了什么。我的 package.json

中有一个 Node.js 项目,其中包含以下内容
  "scripts": {
    "test": "mocha --recursive ./src/setup/*.js ./test/**/*.js"
  },
  "dependencies": {
     "mocha": "^2.2.5"
  }

当我 运行 'npm test' mocha 测试 运行 正确时:

$ npm test (successful run)

然而,当我尝试 运行 我的 package.json

中的 mocha 命令时
$ mocha --recursive ./src/setup/*.js ./test/**/*.js"

此错误为:

-sh: mocha: command not found

我没有全局安装 mocha,我只是通过 npm 安装到这个特定项目。

如果我在全局安装 mocha 就可以了。为什么当我在当前目录的 node_modules 中简单地安装了 mocha 时它不起作用,但它在 'npm test' 中起作用?

npm 脚本自动将 mocha 添加到 PATH:

If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.

https://docs.npmjs.com/misc/scripts#path