- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在寻找一种模式来避免在使用 Node 时需要全局包,我想使用 npm install
安装我需要的所有东西,然后使用 运行每个命令>npm run xxx
,没有安装任何全局包。
例如,我已经配置好运行我的测试。
这些是我的 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 首先在 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 test
那么,实现它的正确方法是什么?我如何告诉 npm 将 jest 安装到 node_modules/.bin 并在我的脚本中引用它时使用它?
最佳答案
似乎比预期的要容易,我只需要发出:
npm install --only=dev
似乎默认 npm 不会安装开发依赖
我做了更多测试,使用 NODE_ENV var,取消设置后 npm install
似乎也安装了 devDependencies,以及 node_modules/.bin/jest 中的 jest。它似乎以某种方式假设我处于生产模式。
我学到的另一个避免安装全局依赖项的技巧是使用 --save-dev 安装它,然后添加一个脚本以使用 npm run
运行它。例如,要避免全局安装 jest 但仍然能够从命令行使用它,您应该:
npm install jest --save-dev
然后将以下内容添加到您的 package.json
scripts: {
"jest": "jest"
}
然后您可以使用 npm run jest
从命令行运行它。要从命令行传递参数,您必须在参数前添加一个“--”,如下所示:npm run jest -- --coverage
。或者您可以只发出 npx jest --coverage
,如果已安装,npx 将使用 node_modules/.bin 中的 jest。 (查看 this 了解更多信息)
顺便说一句,这个answer对类似的问题可能会有用
关于node.js - Node : how to avoid installing global packages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55097434/
我正在尝试使用 JAXB 从 XSD 生成 java 类。 XSD 是我公司的官方 xsd,因此我无法仅为我的项目修改它们。在该网站上进行了数百次尝试和搜索后,我决定直接提出问题。 我的 XSD 中有
我已经了解了以下链接中的详细信息,但仍然存在何时使用哪个文件的问题?https://docs.npmjs.com/files/package-lock.json 最佳答案 包.json 包含项目的相关
当我在 centos 上运行命令 rpmbuild -bb mypackage.spec 时,出现错误 error: Package already exists: %package debuginf
my.packages 是 src 目录中的自定义原型(prototype)包。 Plone 实例中的数千个项目与其类型一起添加。我想将包重命名为 my.package。通过简单地卸载 my.pack
根据 javadoc 规范,我在相关包的根目录中放置了一个名为 package-info.html 的文档文件。但是,当我在该文件夹上运行 Doxygen 时,不会拾取该文件中的文档。我如何告诉 Do
我已经定义了如下的包: (defpackage :thehilariouspackageofamirteymuri (:nicknames ampack amir teymuri) (:use
我正在思考这个问题: > .packages() > (.packages()) [1] "stats" "graphics" "grDevices" "utils" "datase
我在内存中有一个 System.IO.Packaging.Package(它是一个 WordprocessingDocument)并且想将它流式传输到浏览器以保存它。 Word 文档已被基于 MVC
即使这是我不常发现的东西,在成员之前注释/* package*/的原因是什么? /* package */ final void attach(Context context) { atta
我正在开发我的应用程序,但在添加包以便导入它时,我总是收到此错误。 error: type 'Package.Dependency' has no member 'Package' 这是我的 Pack
install.packages("data.table") trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.
放置手动创建的插件的最佳位置是什么: a) C:\Users\{UserName}\AppData\Roaming\Sublime Text 3\Packages 或 b) C:\Users\{Use
这是一个有趣的 Perl 行为。 (至少对我来说 :) ) 我有两个包 PACKAGE1 和 PACKAGE2,它们导出具有相同名称的函数 Method1()。 由于将有如此多的包将导出相同的功能,使
package-archives (("marmalade" . "http://marmalade-repo.org/packages/") ("gnu" . "http://elpa.gnu.or
任何人都可以让我知道 package-lock.json 文件的确切用途吗? 尽管许多人提到它用于查看版本化依赖树。 寻找简单易行的解释。 提前致谢。 最佳答案 npm install使用此文件来确保
Python documentation说 Consider this code: import sound.effects.echo import sound.effects.surround fr
我在 ubuntu 上运行 VPS: Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 C
我有这样一个结构 $ tree -h . ├── [1.0K] myproj │ ├── [ 0] index.py │ ├── [ 0] __init__.py │ └──
我正在尝试解压 System.IO.Packaging.Package我从网络服务器收到的。也就是说,我正在使用 System.IO.Packaging.Package.Open(Stream)方法并
关于 package.json 文件中的@types 依赖项,我有一个愚蠢的问题: 在下面的 URL 中解释了应该安装的类型作为运行时依赖 npm install --save @types/loda
我是一名优秀的程序员,十分优秀!