npm install的--save选项是什么?

npm 5.0.0及更高版本默认将模块作为依赖项添加,--save选项不再是必需的。它曾用于自动将包添加到dependencies部分。同时,--save-dev和--save-prod分别用于将包添加到devDependencies和dependencies。如果没有package.json文件,npm install会创建它并更新依赖列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:What is the --save option for npm install?

I saw some tutorial where the command was: 我看到了一些命令所在的教程:

npm install --save

What does the --save option mean? --save选项是什么意思?

Not able to find the answer on Google. 在Google上找不到答案。


#1楼

参考:https://stackoom.com/question/1K9L2/npm-install的-save选项是什么


#2楼

Update npm 5: 更新npm 5:

As of npm 5.0.0 , installed modules are added as a dependency by default, so the --save option is no longer needed. npm 5.0.0开始 ,默认情况下已安装的模块作为依赖项添加,因此不再需要--save选项。 The other save options still exist and are listed in the documentation for npm install . 其他保存选项仍然存在,并在npm install文档中列出。

Original answer: 原始答案:

Before version 5, NPM simply installed a package under node_modules by default. 在版本5之前,默认情况下,NPM只是在node_modules下安装了一个软件包。 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 dependencies section of your package.json . 当您尝试为应用程序/模块安装依赖项时,您需要先安装它们,然后将它们(以及适当的版本号)添加到package.jsondependencies部分。

The --save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step. --save选项指示NPM自动将软件包包括在package.jsondependencies部分中,从而为您节省了额外的步骤。

In addition, there are the complementary options --save-dev and --save-optional which save the package under devDependencies and optionalDependencies , respectively. 此外,还有补充选项--save-dev--save-optional ,它们分别将软件包保存在devDependenciesoptionalDependencies下。 This is useful when installing development-only packages, like grunt or your testing library. 在安装仅限开发的软件包(例如grunt或测试库)时,这很有用。


#3楼

It won't do anything if you don't have a package.json file. 如果没有package.json文件,它将不会执行任何操作。 Start by running npm init to create one. 首先运行npm init创建一个。 Then calls to npm install --save or npm install --save-dev or npm install --save-optional will update the package.json to list your dependencies. 然后调用npm install --savenpm install --save-devnpm install --save-optional将更新package.json以列出您的依赖项。


#4楼

To add package in dependencies: 要添加依赖包:

npm install my_dep --save

or 要么

npm install my_dep -S

or 要么

npm i my_dep -S

To add package in devDependencies 在devDependencies中添加软件包

npm install my_test_framework --save-dev

or 要么

npm install my_test_framework -D

or 要么

npm i my_test_framework -D

package.json package.json 在此处输入图片说明


#5楼

You can also use -S , -D or -P which are equivalent of saving the package to an app dependency, a dev dependency or prod dependency. 您还可以使用-S-D-P ,它们等效于将程序包保存为应用程序依赖,开发人员依赖或产品依赖。 See more NPM shortcuts below: 请在下面查看更多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

#6楼

npm install package_x --save

The given package (package_x) will be saved in package.json inside dependencies. 给定的包(package_x)将保存在依赖关系内的package.json中。 if you add 如果您添加

npm install <<package_x>> --save-dev

then it will be saved inside devDependencies . 然后将其保存在devDependencies中

### 可能的原因分析 当执行 `npm install --save-dev stream-http` 出现错误时,可能涉及以下几个方面: 1. **网络问题**:NPM 的默认镜像源位于国外服务器上,可能会因为网络不稳定而导致下载失败[^3]。 2. **依赖冲突**:目标包 `stream-http` 所需的某些依赖版本与其他项目中的依赖不兼容,从而引发安装错误[^4]。 3. **Node.js 版本不适配**:部分 NPM 包仅支持特定范围内的 Node.js 版本。如果当前使用的 Node.js 版本过低或过高,则可能导致无法正常安装该包及其依赖项[^5]。 ### 解决方案 #### 方法一:切换国内镜像源 为了规避因网络原因造成的安装失败,可尝试临时更换为淘宝 NPM 镜像或其他更稳定的国内镜像服务: ```bash $ npm install --registry=https://registry.npmmirror.com --save-dev stream-http ``` 或者永久更改全局配置文件来指定新的注册表地址: ```bash $ npm config set registry https://registry.npmmirror.com/ ``` 完成设置之后再次运行原命令即可验证效果[^6]。 #### 方法二:清理缓存并重试 有时本地存在的旧版数据也可能干扰新请求的成功处理,因此建议先清除相关记录后再继续操作: ```bash $ npm cache clean --force $ npm install --save-dev stream-http ``` #### 方法三:检查 Node.jsNPM 版本匹配情况 确认所用环境是否满足官方文档里提到的要求。可以通过如下方式查询现有状态以及升级必要组件至最新稳定发行版: ```bash $ node -v && npm -v # 如果发现版本较低则考虑更新工具链 $ nvm install --lts # 使用nvm管理node版本推荐做法之一 ``` #### 方法四:强制忽略 peerDependency 警告(谨慎使用) 对于一些非致命性的警告消息,我们也可以通过增加额外选项让其不影响最终结果呈现出来: ```bash $ npm install --legacy-peer-deps --save-dev stream-http ``` 此方法适用于那些由于严格模式开启而阻止了原本能够工作的场景下的快速修复手段[^7]。 --- ### 总结说明 上述四种途径分别针对不同层面的因素提供了对应的调整策略,在实际应用过程中可以根据具体情况灵活选用其中一种或多组合起来解决问题。同时提醒开发者注意保持良好的开发习惯比如定期同步最新的依赖清单、合理规划项目的结构设计等措施有助于减少此类异常发生的概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值