node.js --save 和 --save-dev 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22891211/
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
What is the difference between --save and --save-dev?
提问by nfort
What is the difference between:
有什么区别:
npm install [package_name] --save
and:
和:
npm install [package_name] --save-dev
What does this mean?
这是什么意思?
采纳答案by Tuong Le
--save-devis used to save the package for development purpose. Example: unit tests, minification..--saveis used to save the package required for the application to run.
--save-dev用于保存包以用于开发目的。示例:单元测试、缩小..--save用于保存应用程序运行所需的包。
回答by Michael Bruce
The difference between --saveand --save-devmay not be immediately noticeable if you have tried them both on your own projects. So here are a few examples...
之间的区别--save和--save-dev你们有没有试过他们都在自己的项目可能不会立即明显。所以这里有几个例子......
Lets say you were building an app that used the momentpackage to parse and display dates. Your app is a scheduler so it really needs this package to run, as in: cannot run without it. In this case you would use
假设您正在构建一个使用moment包来解析和显示日期的应用程序。您的应用程序是一个调度程序,因此它确实需要此程序包才能运行,例如:没有它就无法运行。在这种情况下,您将使用
npm install moment --save
This would create a new value in your package.json
这将在您的 package.json 中创建一个新值
"dependencies": {
...
"moment": "^2.17.1"
}
When you are developing, it really helps to use tools such as test suites and may need jasmine-coreand karma. In this case you would use
在开发时,使用测试套件等工具确实很有帮助,可能需要jasmine-core和karma。在这种情况下,您将使用
npm install jasmine-core --save-dev
npm install karma --save-dev
This would also create a new value in your package.json
这也会在您的 package.json 中创建一个新值
"devDependencies": {
...
"jasmine-core": "^2.5.2",
"karma": "^1.4.1",
}
You do not needthe test suite to run the app in its normal state, so it is a --save-devtype dependency, nothing more. You can see how if you do not understand what is really happening, it is a bit hard to imagine.
您不需要测试套件在正常状态下运行应用程序,因此它是一个--save-dev类型依赖项,仅此而已。您可以看到如果您不了解真正发生的事情,这有点难以想象。
Taken directly from NPM docs docs#dependencies
直接取自 NPM 文档docs#dependencies
Dependencies
Dependencies are specified in a simple object that maps a package name to a version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL.
Please do not put test harnesses or transpilers in your dependencies object. See devDependencies, below.
依赖关系
依赖项在一个简单的对象中指定,该对象将包名称映射到版本范围。版本范围是一个字符串,它具有一个或多个以空格分隔的描述符。依赖项也可以用 tarball 或 git URL 来标识。
请不要将测试工具或转译器放在您的依赖项对象中。请参阅下面的devDependencies。
Even in the docs, it asks you to use --save-dev for modules such as test harnesses.
即使在文档中,它也要求您将 --save-dev 用于诸如测试工具之类的模块。
I hope this helps and is clear.
我希望这会有所帮助并且很清楚。
回答by Lakshmi Swetha G
By default, NPM simply installs a package under node_modules. When you're trying to install dependencies for your app/module, you would need to first install them, and then add them to the dependenciessection of your package.json.
默认情况下,NPM 只是在 node_modules 下安装一个包。当你试图安装依赖于你的应用程序/模块,你就需要先安装它们,然后将它们添加到dependencies您的部分package.json。
--save-devadds the third-party package to the package's development dependencies. It won't be installed when someone installs your package. It's typically only installed if someone clonesyour source repository and runs npm installin it.
--save-dev将第三方包添加到包的开发依赖项中。当有人安装您的软件包时,它不会被安装。它通常仅在有人克隆您的源存储库并npm install在其中运行时才安装。
--saveadds the third-party package to the package's dependencies. It will be installed together with the package whenever someone runs npm install package.
--save将第三方包添加到包的依赖项中。每当有人运行时,它将与包一起安装npm install package。
Dev dependencies are those dependencies that are only needed for developing the package. That can include test runners, compilers, packagers, etc.
Both types of dependencies are stored in the package's package.jsonfile. --saveadds to dependencies, --save-devadds to devDependencies
开发依赖项是那些仅在开发包时需要的依赖项。这可以包括测试运行程序、编译器、打包程序等。这两种类型的依赖项都存储在包的package.json文件中。--save添加到dependencies,--save-dev添加到devDependencies
npm installdocumentation can be referred here.
npm install文档可以参考这里。
回答by Hymanalope
A perfect example of this is:
一个完美的例子是:
$ npm install typescript --save-dev
In this case, you'd want to have Typescript (a javascript-parseable coding language) available for development, but once the app is deployed, it is no longer necessary, as all of the code has been transpiled to javascript. As such, it would make no sense to include it in the published app. Indeed, it would only take up space and increase download times.
在这种情况下,您希望 Typescript(一种 javascript 可解析的编码语言)可用于开发,但是一旦部署了应用程序,它就不再需要了,因为所有代码都已转换为 javascript。因此,将其包含在已发布的应用程序中是没有意义的。事实上,它只会占用空间并增加下载时间。
回答by Aritra Chakraborty
Let me give you an example,
让我给你举个例子,
- You are a developer of a very SERIOUSnpmlibrary. Which uses different testing libraries to test the package.
- An user Downloaded your library and want to use it in their code. Do they need to download your testing libraries as well? Maybe you use
jestfor testing and they usemocha. Do you want them to installjestas well? JustTo run your library?
- 你是一个非常严肃的npm库的开发者。它使用不同的测试库来测试包。
- 用户下载了您的库并希望在他们的代码中使用它。他们是否也需要下载您的测试库?也许您
jest用于测试而他们使用mocha. 您也希望它们安装jest吗?只是为了运行你的图书馆?
No. right? That's why they are in devDependencies.
没有权利?这就是为什么他们在devDependencies.
When someone does, npm i yourPackageonly the libraries required to RUNyour library will be installed. Other libraries you used to bundle your code with or testing and mocking will not be installed because you put them in devDependencies. Pretty neat right?
当有人这样做时,npm i yourPackage只会安装运行您的库所需的库。用于捆绑代码或测试和模拟的其他库将不会安装,因为您将它们放在devDependencies. 很整洁吧?
So, Whydo the developers need to expose the devDependancies?
那么,为什么开发人员需要公开devDependencies呢?
Let's say your package is an open source package and 100s of people are sending pull requests to your package. Then how they will test the package? They will git cloneyour repo and when they would do an npm ithe dependenciesas well as devDependencies.
Because they are not using your package. They are developing the package further, thus, in order to test your package they need to pass the existing test cases as well write new. So, they need to use your devDependencieswhich contain all the testing/building/mocking libraries that YOU used.
假设您的包是一个开源包,并且有 100 多人向您的包发送拉取请求。那么他们将如何测试这个包呢?他们将git clone你的回购时,他们会做一个npm i的依赖以及devDependencies。
因为他们没有使用你的包。他们正在进一步开发包,因此,为了测试您的包,他们需要通过现有的测试用例并编写新的测试用例。因此,他们需要使用您的devDependencies,其中包含您使用的所有测试/构建/模拟库。
回答by wayfarer_boy
As suggested by @andreas-hultgren in this answerand according to the npm docs:
正如@andreas-hultgren 在这个答案中和根据npm 文档所建议的:
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.
如果有人计划在他们的程序中下载和使用您的模块,那么他们可能不想或不需要下载和构建您使用的外部测试或文档框架。
However, for webapp development, Yeoman(a scaffolding tool that installs a peer-reviewed, pre-written package.json file amongst other things) places all packages in devDependencies and nothing in dependencies, so it appears that the use of --save-devis a safe bet in webappdevelopment, at least.
但是,对于 webapp 开发,Yeoman(安装经过同行评审的预先编写的 package.json 文件等的脚手架工具)将所有包放在 devDependencies 中,而在依赖项中没有任何内容,因此看起来使用--save-dev是一个安全的赌注至少在webapp开发中。
回答by alex
--save-devsaves semver spec into "devDependencies" array in your package descriptor file, --savesaves it into "dependencies" instead.
--save-dev将 semver 规范保存到包描述符文件中的“devDependencies”数组中,--save而是将其保存到“dependencies”中。
回答by Alireza
Clear answers are already provided. But it's worth mentioning how devDependenciesaffects installing packages:
已经提供了明确的答案。但值得一提的是如何devDependencies影响安装包:
By default, npm install will install all modules listed as dependencies in package.json . With the --production flag (or when the NODE_ENV environment variable is set to production ), npm will not install modules listed in devDependencies .
默认情况下, npm install 将安装 package.json 中列为依赖项的所有模块。使用 --production 标志(或当 NODE_ENV 环境变量设置为 production 时),npm 将不会安装 devDependencies 中列出的模块。
回答by velhala
You generally don't want to bloat production package with things that you only intend to use for Development purposes.
您通常不希望使用仅打算用于开发目的的东西来膨胀生产包。
Use --save-dev(or -D) option to separate packages such as Unit Test frameworks (jest, jasmine, mocha, chai, etc.)
使用--save-dev(或-D) 选项将单元测试框架(jest、jasmine、mocha、chai 等)等包分开
Any other packages that your app needs for Production, should be installed using --save(or -S).
您的应用程序需要用于生产的任何其他包,应使用--save(或-S)安装。
npm install --save lodash //prod dependency
npm install -S moment // " "
npm install -S opentracing // " "
npm install -D jest //dev only dependency
npm install --save-dev typescript //dev only dependency
If you open the package.jsonfile then you will see these entries listed under two different sections:
如果您打开该package.json文件,您将看到这些条目列在两个不同的部分下:
"dependencies": {
"lodash": "4.x",
"moment": "2.x",
"opentracing": "^0.14.1"
},
"devDependencies": {
"jest": "22.x",
"typescript": "^2.8.3"
},
回答by Biswadev
--save-devis used for modules used in development of the application,not require while running it in production envionment --saveis used to add it in package.json and it is required for running of the application.
--save-dev用于应用程序开发中使用的模块,在生产环境中运行时不需要 --save用于将它添加到 package.json 中,它是应用程序运行所必需的。
Example: express,body-parser,lodash,helmet,mysql all these are used while running the application use --save to put in dependencies while mocha,istanbul,chai,sonarqube-scanner all are used during development ,so put those in dev-dependencies .
示例:express,body-parser,lodash,helmet,mysql 所有这些都在运行应用程序时使用,使用 --save 放入依赖项,而 mocha,istanbul,chai,sonarqube-scanner 都在开发过程中使用,所以将它们放在 dev 中-依赖关系。
npm link or npm install will also install the dev-dependency modules along with dependency modules in your project folder
npm link 或 npm install 也会在你的项目文件夹中安装 dev-dependency 模块和依赖模块

