为什么我无法运行与 package.json 文件中完全相同的命令? 这是一个例子:
Package.json
"scripts": {
...
"test": "jest",
...
},
如果我运行“npm run test”,jest 工作正常并且我的单元测试被执行。 但是,如果我尝试在不使用 package.json 中的脚本的情况下运行 jest,则无法执行它。
当我运行命令 jest 时:
zsh: command not found: jest
我认为当我在项目的目录中时,我将能够使用与项目相同的脚本,而不必将其添加到脚本中并通过 npm 或yarn 调用它。
最佳答案
实际上,如果你不能通过输入 jest 的名称来使用 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 将更新您访问 jest 的路径,该路径在 npm 安装时导出到 node_modules/.bin 目录
中。
关于node.js - 为什么我无法在不调用脚本的情况下运行与我的 package.json 脚本相同的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70682822/