本文翻译自:npm install vs. update - what's the difference?
What is the practical difference between npm install
and npm update
? npm install
和npm 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 install和npm 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 另外 :默认情况下, install
和update
处理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
) usingnpm 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
将运行各种脚本: prepublish
, preinstall
, install
, postinstall
。 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中定义的节点模块(不更新)