【发布时间】:2019-08-01 12:08:41
【问题描述】:
我正在寻找一种模式来避免在使用节点时需要全局包,我想用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
[...]
如果我使用npm install -g jest 全局安装 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",
但是每次我运行npm run testnpx 安装 jest 需要超过 10 秒的时间@
那么,实现它的正确方法是什么? O 如何告诉 npm 将 jest 安装到 node_modules/.bin 并在我的脚本中引用它时使用它?
【问题讨论】:
-
再次检查你的
NODE_ENV环境变量,如果是production,那么npm install命令只会下载依赖包。最后,确保NODE_ENV !== production,然后再次运行npm install命令(我的期望:jest和nodemon将出现在您的node_modules/.bin/中) -
NODE_ENV 为空,但我可以告诉 npm 使用 --only=dev 选项安装 devDepencies
-
奇怪的事情,现在我做了几个测试,将 NODE_ENV 设置为开发,并取消设置它,现在
npm install安装 devDependecies,就像你说的那样
标签: node.js npm jestjs npm-scripts npx