我正在寻找一种模式来避免在使用节点时需要全局包,我想安装我需要的所有东西npm install
,然后只使用 运行每个命令npm run xxx
,而不安装任何全局包。
例如,我已将 jest 配置为运行我的测试。
这些是我的 package.json 中的依赖项:
[...]
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.16.1",
"@types/node": "^11.10.5",
"express": "^4.16.4",
"ts-node-dev": "^1.0.0-pre.32",
"typescript": "^3.3.3333"
},
"devDependencies": {
"@types/jest": "^24.0.9",
"@types/supertest": "^2.0.7",
"jest": "^24.3.1",
"nodemon": "^1.18.10",
"supertest": "^4.0.0",
"ts-jest": "^24.0.0"
}
[...]
这些是我配置的一些脚本:
[...]
"scripts": {
"test": "jest --coverage",
"tsc": "tsc",
"watch": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/server.ts"
},
[...]
但是当我发出npm run test
这个错误时:
$ npm run test
> ci-test@0.0.1 test /home/sas/devel/apps/vue/ci-test
> jest --coverage
sh: 1: jest: not found
npm ERR! file sh
[...]
如果我在全球范围内安装 jest 并且npm install -g jest
一切运行良好,但这正是我想要避免的。
我做出的一些假设可能是错误的:
运行脚本时,节点首先在 node_modules/.bin 中查找命令(以便使用本地安装的包)
当我发出
npm install
每个命令行命令时,都会安装到 node_modules/.bin
最后一个不起作用,因为即使我的 devDependencies 中有 jest,我的项目中也没有 node_modules/.bin/jest 文件
$ ls node_modules/.bin/
acorn cdl esgenerate esvalidate is-ci json5 loose-envify mime nodetouch parser semver sshpk-sign strip-indent watch
atob escodegen esparse import-local-fixture jsesc js-yaml marked mkdirp nopt rc sshpk-conv sshpk-verify uglifyjs
另一方面,作为一种解决方法,以下似乎可行:
"scripts": {
"test": "npx jest --coverage",
但是每次我运行 npx 安装 jest 需要超过 10 秒npm run test
那么,实现它的正确方法是什么?O 如何告诉 npm 将 jest 安装到 node_modules/.bin 并在我的脚本中引用它时使用它?