node.js npm install 的 --save 选项是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19578796/
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 --save option for npm install?
提问by Dmitri
I saw some tutorial where the command was:
我看到了一些教程,其中命令是:
npm install --save
What does the --saveoption mean?
什么是--save选项是什么意思?
Not able to find the answer on Google.
无法在 Google 上找到答案。
回答by voithos
Update npm 5:
更新 npm 5:
As of npm 5.0.0, installed modules are added as a dependency by default, so the --saveoption is no longer needed. The other save options still exist and are listed in the documentationfor npm install.
从npm 5.0.0 开始,已安装的模块默认添加为依赖项,因此--save不再需要该选项。其他保存选项依然存在并在中列出的文件的npm install。
Original answer:
原答案:
Before version 5, NPM simply installed a package under node_modulesby default. When you were trying to install dependencies for your app/module, you would need to first install them, and then add them (along with the appropriate version number) to the dependenciessection of your package.json.
在版本 5 之前,NPM 只是在node_modules默认情况下安装了一个包。当您尝试为您的应用程序/模块安装依赖项时,您需要先安装它们,然后将它们(连同适当的版本号)添加到dependencies您的package.json.
The --saveoption instructed NPM to include the package inside of the dependenciessection of your package.jsonautomatically, thus saving you an additional step.
该--save选项指示 NPM自动将包包含在dependencies您的部分中package.json,从而为您节省了额外的步骤。
In addition, there are the complementary options --save-devand --save-optionalwhich save the package under devDependenciesand optionalDependencies, respectively. This is useful when installing development-only packages, like gruntor your testing library.
此外,还有补充选项--save-dev和--save-optional分别将包保存在devDependencies和下optionalDependencies。这在安装仅用于开发的软件包时很有用,例如grunt或您的测试库。
回答by Joe L.
回答by Nick Retallack
It won't do anything if you don't have a package.jsonfile. Start by running npm initto create one. Then calls to npm install --saveor npm install --save-devor npm install --save-optionalwill update the package.jsonto list your dependencies.
如果你没有package.json文件,它不会做任何事情。首先运行npm init以创建一个。然后调用npm install --save或npm install --save-dev或npm install --save-optional将更新package.json,列出你的依赖。
回答by ROROROOROROR
回答by DevWL
You can also use -S, -Dor -Pwhich are equivalent of saving the package to an app dependency, a dev dependency or prod dependency. See more NPM shortcuts below:
您还可以使用-S,-D或-P相当于将包保存到应用程序依赖项、开发依赖项或生产依赖项。在下面查看更多 NPM 快捷方式:
-v: --version
-h, -?, --help, -H: --usage
-s, --silent: --loglevel silent
-q, --quiet: --loglevel warn
-d: --loglevel info
-dd, --verbose: --loglevel verbose
-ddd: --loglevel silly
-g: --global
-C: --prefix
-l: --long
-m: --message
-p, --porcelain: --parseable
-reg: --registry
-f: --force
-desc: --description
-S: --save
-P: --save-prod
-D: --save-dev
-O: --save-optional
-B: --save-bundle
-E: --save-exact
-y: --yes
-n: --yes false
ll and la commands: ls --long
This list of shortcuts can be obtained by running the following command:
可以通过运行以下命令获取此快捷方式列表:
$ npm help 7 config
回答by themefield
As of npm 5, it is more favorable to use --save-prod(or -P) than --savebut doing the same thing, as is stated in npm install. So far, --savestill works if provided.
从 npm 5 开始,使用--save-prod(or -P) 比做--save同样的事情更有利,如npm install 中所述。到目前为止,--save如果提供仍然有效。
回答by rajesh kumar
As of npm 5, npm will now save by default. In case,if you would like npm to work in a similar old fashion (no autosave) to how it was working in previous versions, you can update the config option to enable autosave as below.
从 npm 5 开始,npm 现在将默认保存。如果您希望 npm 以与以前版本中的工作方式类似的旧方式(无自动保存)工作,您可以更新配置选项以启用自动保存,如下所示。
npm config set save false
To get the current setting, you can execute the following command:
要获取当前设置,您可以执行以下命令:
npm config get save
回答by Nizam Deen
npm install package_x --save
npm install package_x --save
The given package (package_x) will be saved in package.json inside dependencies. if you add
给定的包 (package_x) 将保存在依赖项内的 package.json 中。如果你添加
npm install <<package_x>> --save-dev
npm install <<package_x>> --save-dev
then it will be saved inside devDependencies.
然后它将保存在devDependencies 中。
回答by Rubin bhandari
The easier (and more awesome) way to add dependencies to your package.json is to do so from the command line, flagging the npm install command with either --save or --save-dev, depending on how you'd like to use that dependency.
将依赖项添加到 package.json 的更简单(也更棒)的方法是从命令行执行此操作,使用 --save 或 --save-dev 标记 npm install 命令,具体取决于您想要的方式使用该依赖项。
回答by Sunny Goel
npm install --saveor npm install --save-devwhy we choose 1 options between this two while installing package in our project.
npm install --save或者npm install --save-dev为什么我们在我们的项目中安装包时在这两个选项之间选择 1 个选项。
things is clear from the above answers that npm install --savewill add entry in the dependencyfield in pacakage.jsonfile and other one in dev-dependency.
从上面的答案中npm install --save可以清楚地看出,这将dependency在pacakage.json文件中的字段中添加条目,在dev-dependency.
So question arises why we need entry of our installing module in pacakge.json file because whenever we check-in code in gitor giving our code to some one we always give it or check it without node-modulesbecause it is very large in size and also available at common place so to avoid this we do that.
所以问题出现了为什么我们需要在 pacakge.json 文件中输入我们的安装模块,因为每当我们签入代码git或将我们的代码提供给某人时,我们总是提供或不检查它,node-modules因为它的大小非常大并且也可以在常见的地方,所以为了避免这种情况,我们这样做。
so then how other person will get all the modules that is specifically or needed for that project so answers is from the package.jsonfile that have the entry of all the required packages for running or developing that project.
那么其他人如何获得该项目专门或需要的所有模块,所以答案是from the package.json包含运行或开发该项目所需的所有包的条目的文件。
so after getting the code we simply need to run the npm installcommand it will read the package.json file and install the necessary required packages.
所以在获得代码we simply need to run the npm install命令后,它将读取 package.json 文件并安装必要的必需包。


