npm install vs. update - 有什么区别?

本文探讨了npm install和npm update的区别。npm install用于安装package.json中列出的模块及其依赖,而npm update则更新package.json目录下的所有包及其依赖。若已安装模块,npm update将获取新版本。此外,npm install可以手动安装node_modules,设置全局模块,安装特定git版本,从git URL安装,以及在某些情况下强制重新安装。
摘要由CSDN通过智能技术生成

本文翻译自:npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update ? npm installnpm update之间有什么实际区别? When should I use which? 我什么时候应该使用哪个?


#1楼

参考:https://stackoom.com/question/qMH1/npm-install-vs-update-有什么区别


#2楼

npm install installs all modules that are listed on package.json file and their dependencies. npm install安装package.json文件及其依赖项中列出的所有模块。

npm update updates all packages in the node_modules directory and their dependencies. npm update更新node_modules目录中的所有包及其依赖项。

npm install express installs only the express module and its dependencies. npm install express只安装express模块​​及其依赖项。

npm update express updates express module (starting with npm@2.x, it doesn't update its dependencies). npm update express updates express模块​​(从npm@2.x开始,它不会更新其依赖项)。

So updates are for when you already have the module and wish to get the new version. 因此,更新适用于已有模块并希望获得新版本的更新。


#3楼

The difference between npm install and npm update handling of package versions specified in package.json : package.json中指定的软件包版本的npm installnpm update处理之间的区别:

{
  "name":          "my-project",
  "version":       "1.0",                             // install   update
  "dependencies":  {                                  // ------------------
    "already-installed-versionless-module":  "*",     // ignores   "1.0" -> "1.1"
    "already-installed-semver-module":       "^1.4.3" // ignores   "1.4.3" -> "1.5.2"
    "already-installed-versioned-module":    "3.4.1"  // ignores   ignores
    "not-yet-installed-versionless-module":  "*",     // installs  installs
    "not-yet-installed-semver-module":       "^4.2.1" // installs  installs
    "not-yet-installed-versioned-module":    "2.7.8"  // installs  installs
  }
}

Summary : The only big difference is that an already installed module with fuzzy versioning ... 总结 :唯一的区别是已安装的模块具有模糊版本 ...

  • gets ignored by npm install npm install忽略
  • gets updated by npm update npm update

Additionally : install and update by default handle devDependencies differently 另外 :默认情况下, installupdate处理devDependencies的方式不同

  • npm install will install/update devDependencies unless --production flag is added 除非添加了--production标志,否则npm install安装/更新 devDependencies
  • npm update will ignore devDependencies unless --dev flag is added 除非添加了--dev标志,否则npm update忽略 devDependencies

Why use npm install at all? 为什么要使用npm install

Because npm install does more when you look besides handling your dependencies in package.json . 因为除了在package.json处理依赖项之外, npm install做的更多。 As you can see in npm install you can ... 正如你在npm安装中看到的,你可以......

  • manually install node-modules 手动安装节点模块
  • set them as global (which puts them in the shell's PATH ) using npm install -g <name> 使用npm install -g <name>将它们设置为全局 (将它们置于shell的PATH
  • install certain versions described by git tags 安装git标签描述的某些版本
  • install from a git url 从git url安装
  • force a reinstall with --force --force强制重新安装

#4楼

In most cases, this will install the latest version of the module published on npm. 在大多数情况下,这将安装在npm上发布的最新版本的模块。

npm install express --save

or better to upgrade module to latest version use: 或者更好地将模块升级到最新版本使用:

npm install express@latest --save --force

--save : Package will appear in your dependencies. --save :包将出现在您的依赖项中。

More info: npm-install 更多信息: npm-install


#5楼

Many distinctions have already been mentioned. 已经提到了许多区别。 Here is one more: 还有一个:

Running npm install at the top of your source directory will run various scripts: prepublish , preinstall , install , postinstall . 在源目录顶部运行npm install将运行各种脚本: prepublishpreinstallinstallpostinstall Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies. 根据这些脚本的作用, npm install可能比安装依赖项做更多的工作。

I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. 我刚刚有一个用例,其中prepublish将调用make ,而Makefile旨在获取依赖关系,如果package.json已更新。 Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly. Makefile调用npm install会导致无限递归,而调用npm update工作正常,安装所有依赖项,以便即使直接调用make也可以继续构建。


#6楼

npm update : install and update with latest node modules which are in package.json npm update :使用package.json中的最新节点模块安装和更新

npm install : install node modules which are defined in package.json(without update) npm install :安装在package.json中定义的节点模块(不更新)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值