如何防止为 Node.js (package.json) 安装“devDependencies”NPM 模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9268259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you prevent install of "devDependencies" NPM modules for Node.js (package.json)?
提问by Lance Pollard
I have this in my package.json file (shortened version):
我的 package.json 文件中有这个(缩短版):
{
"name": "a-module",
"version": "0.0.1",
"dependencies": {
"coffee-script": ">= 1.1.3"
},
"devDependencies": {
"stylus": ">= 0.17.0"
}
}
I am using NPM version 1.1.1 on Mac 10.6.8.
我在 Mac 10.6.8 上使用 NPM 版本 1.1.1。
When I run the following command from the project root, it installs both the dependenciesanddevDependencies:
当我从项目根目录运行以下命令时,它会安装dependencies和devDependencies:
npm install
I was under the impression that this command installed the devDependencies:
我的印象是这个命令安装了devDependencies:
npm install --dev
How do I make it so npm installonly installs dependencies(so production environment only gets those modules), while something like npm install --devinstalls both dependenciesand devDependencies?
我如何让它npm install只安装dependencies(所以生产环境只获取这些模块),而像npm install --dev安装dependencies和 之类的东西devDependencies?
回答by Rohan Singh
The npm installcommand will install the devDependenciesalong other dependencieswhen run inside a package directory, in a development environment (the default).
在开发环境(默认)中的包目录中运行时,该npm install命令将安装devDependencies其他命令dependencies。
Use npm install --only=prod(or --only=production) to install onlydependencies, and not devDependencies,regardless of the value of the NODE_ENVenvironment variable.
使用npm install --only=prod(或--only=production)只安装dependencies,而不devDependencies,考虑NODE_ENV环境变量的值。
Source: npm docs
来源:npm 文档
Note:Before v3.3.0 of npm (2015-08-13), the option was called --production, i.e. npm install --production.
注意:在 npm (2015-08-13) v3.3.0 之前,该选项被称为--production,即npm install --production。
回答by smertrios
I run into that problem too! npm install is somewhat confusing and web posts keep bringing in the -d/--dev flags as if there is an explicit 'development' install mode.
我也遇到这个问题!npm install 有点令人困惑,网络帖子不断引入 -d/--dev 标志,好像有一个明确的“开发”安装模式。
npm installwill install both "dependencies" and "devDependencies"npm install --productionwill only install "dependencies"npm install --devwill only install "devDependencies"
npm install将同时安装“ dependencies”和“ devDependencies”npm install --production只会安装“依赖项”npm install --dev只会安装“ devDependencies”
回答by Cloxure
The new option is:
新选项是:
npm install --only=prod
If you want to install only devDependencies:
如果您只想安装 devDependencies:
npm install --only=dev
回答by wzr1337
If you read this POST in 2016, please achieve what you want by using
如果您在 2016 年阅读了这篇文章,请使用
--only={prod[uction]|dev[elopment]}
argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV.
参数将导致只安装 devDependencies 或只安装非 devDependencies,而不管 NODE_ENV。
回答by piercebot
If you have already installed all your dependencies, and you want to avoid having to download your production packages from NPM again, you can simply type:
如果您已经安装了所有依赖项,并且希望避免再次从 NPM 下载生产包,您只需键入:
npm prune --production
This will remove your dev dependencies from your node_modulesfolder, which is helpful if you're trying to automate a two step process like
这将从您的node_modules文件夹中删除您的开发依赖项,如果您尝试自动化两步过程,这将很有帮助,例如
- Webpack my project, using dev dependencies
- Build a Docker image using only production modules
- Webpack 我的项目,使用开发依赖项
- 仅使用生产模块构建 Docker 映像
Running npm prunein between will save you from having to reinstall everything!
npm prune在两者之间运行将使您不必重新安装所有东西!
回答by creatovisguru
When using "npm install" the modules are loaded and available throughout your application regardless of if they are "devDependencies" or "dependencies". Sum of this idea: everything which your package.json defines as a dependency (any type) gets installed to node_modules.
使用“npm install”时,模块会在整个应用程序中加载并可用,无论它们是“devDependencies”还是“dependencies”。这个想法的总和:你的 package.json 定义为依赖项(任何类型)的所有东西都被安装到 node_modules。
The purpose for the difference between dependencies/devDependencies/optionalDependencies is what consumers of your code can do w/ npm to install these resources.
依赖项/devDependencies/optionalDependencies 之间差异的目的是代码的使用者可以使用 npm 来安装这些资源。
Per the documentation: https://npmjs.org/doc/json.html...
根据文档:https: //npmjs.org/doc/json.html...
If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.
In this case, it's best to list these additional items in a devDependencies hash.
These things will be installed whenever the --dev configuration flag is set. This flag is set automatically when doing npm link or when doing npm install from the root of a package, and can be managed like any other npm configuration param. See config(1) for more on the topic.
如果有人计划在他们的程序中下载和使用您的模块,那么他们可能不想或不需要下载和构建您使用的外部测试或文档框架。
在这种情况下,最好在 devDependencies 哈希中列出这些附加项。
只要设置了 --dev 配置标志,就会安装这些东西。在执行 npm link 或从包的根目录执行 npm install 时,此标志会自动设置,并且可以像任何其他 npm 配置参数一样进行管理。有关该主题的更多信息,请参阅 config(1)。
However, to resolve this question, if you want to ONLY install the "dependencies" using npm, the following command is:
但是,要解决此问题,如果您只想使用 npm 安装“依赖项”,则以下命令是:
npm install --production
This can be confirmed by looking at the Git commit which added this filter (along with some other filters [listed below] to provide this functionality).
这可以通过查看添加此过滤器的 Git 提交来确认(以及提供此功能的其他一些过滤器 [下面列出])。
Alternative filters which can be used by npm:
npm 可以使用的替代过滤器:
--save => updates dependencies entries in the {{{json}}} file
--force => force fetching remote entries if they exist on disk
--force-latest => force latest version on conflict
--production => do NOT install project devDependencies
--no-color => do not print colors
@dmarr try using npm install --production
@dmarr 尝试使用 npm install --production
回答by Kevin Cox
npm will install dev dependencies when installing from inside a package (if there is a package.jsonin the current directory). If it is from another location (npm registry, git repo, different location on the filesystem) it only installs the dependencies.
npm 将在从包内部安装时安装 dev 依赖项(如果package.json当前目录中有一个)。如果它来自另一个位置(npm 注册表、git repo、文件系统上的不同位置),它只会安装依赖项。
回答by Gabriel Schulhof
I have found that, when trying to install dev dependencies for a package that contains a node addon, you cannot avoid building the addon when running npm install --dev even if you just want to install the devDependencies. So, I had to go around npm's back:
我发现,当尝试为包含节点插件的包安装开发依赖项时,即使您只想安装 devDependencies,也无法避免在运行 npm install --dev 时构建插件。所以,我不得不绕过 npm:
node -e 'console.log( Object.keys( require( "./package.json" ).devDependencies ) );' | \
sed -e "s/^[^']*'//" -e "s/'.*$//" | \
xargs npm install
Or, better (and more succinctly) yet,
或者,更好(更简洁),
node -e 'Object.keys( require( "./package.json" ).devDependencies )
.map( function( item ){ console.log( item ) } );' | xargs npm install
回答by Sandip Nirmal
Use npm install packageName --savethis will add package in dependencies, if you use npm install packageName --save-devthen it devDependencies.
使用npm install packageName --save它会在依赖项中添加包,如果你使用npm install packageName --save-dev它devDependencies。
npm install packageName --save-devshould be used for adding packages for development purpose. Like adding TDD packages (Chai, mocha, etc). Which are used in development and not in production.
npm install packageName --save-dev应该用于添加用于开发目的的包。比如添加 TDD 包(Chai、mocha 等)。哪些用于开发而不是生产。
回答by Francesco Casula
It's worth mentioning that you can use the NODE_ENVenvironment variable to achieve the same result. Particularly useful if you're containerizing your Node application (e.g. Docker).
值得一提的是,您可以使用NODE_ENV环境变量来实现相同的结果。如果您正在容器化 Node 应用程序(例如 Docker),则特别有用。
NODE_ENV=production npm install
The above code will install all your dependencies but the dev ones (i.e. devDependencies).
上面的代码将安装您的所有依赖项,但开发的(即devDependencies)。
if you need to use environment variables in your Dockerfilemore information can be found here.
如果您需要在您的Dockerfile更多信息中使用环境变量,可以在这里找到。
Environment variables are easy to overwrite whenever needed (e.g. if you want to run your test suite say on Travis CI). If that were the case you could do something like this:
环境变量很容易在需要时覆盖(例如,如果您想在 Travis CI 上运行测试套件)。如果是这种情况,您可以执行以下操作:
docker run -v $(pwd):/usr/src/app --rm -it -e NODE_ENV=production node:8 npm install
production
- Default: false
Type: Boolean Set to true to run in "production" mode.
- devDependencies are not installed at the topmost level when running local npm install without any arguments.
- Set the NODE_ENV="production" for lifecycle scripts.
生产
- 默认值:假
类型:Boolean 设置为 true 以在“生产”模式下运行。
- 在不带任何参数的情况下运行本地 npm install 时,devDependencies 不会安装在最顶层。
- 为生命周期脚本设置 NODE_ENV="production"。
Happy containerization =)
快乐的容器化=)

