为什么我不能 运行 与我的 package.json 脚本相同的命令而不调用脚本?

Why I can't run the same command that is my script of package.json without calling the script?

为什么我不能 运行 一个与我的 package.json 文件中的命令完全相同的命令? 这是一个例子:

Package.json

"scripts": {
  ...
  "test": "jest",
  ...
},

如果我 运行 "npm 运行 test" jest 工作正常并且我的单元测试被执行。 但是如果我尝试 运行 开玩笑而不使用 package.json 中的脚本,我将无法执行它。

当我运行命令开玩笑时:

zsh: command not found: jest

我以为当我在项目的目录下时,我将能够使用与项目相同的脚本,而不必将其添加到脚本中并通过 npm 或 yarn 调用它。

实际上,如果你不能通过输入它的名字来使用 jest,这意味着 jest 是本地添加的,你可以通过两种方式 运行 locals 命令,一种是 ./node_modules/.bin/jest 将调用jest 命令,第二个 npx jest 也会调用 jest。 对于脚本字段,如 npm documentation:

中所述

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.

因此,当您执行 npm run test 时,npm 将更新您的路径以访问在 npm install 上导出到 node_modules/.bin directory 的 jest。