node.js npm install 与 update - 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12478679/
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
npm install vs. update - what's the difference?
提问by Borek Bernard
What is the practical difference between npm installand npm update? When should I use which?
npm install和之间的实际区别是npm update什么?我什么时候应该使用哪个?
回答by xanderiel
The difference between npm installand npm updatehandling 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 - gets updated by
npm update
- 被忽略
npm install - 被更新
npm update
Additionally: installand updateby default handle devDependencies differently
另外:install和update默认手柄devDependencies不同
npm installwill install/updatedevDependencies unless--productionflag is addednpm updatewill ignoredevDependencies unless--devflag is added
npm install除非添加标志,否则将安装/更新devDependencies--productionnpm update除非添加标志,否则将忽略devDependencies--dev
Why use npm installat all?
为什么要使用npm install?
Because npm installdoes more when you look besides handling your dependencies in package.json.
As you can see in npm installyou can ...
因为npm install除了在package.json. 正如您在npm install 中看到的,您可以...
- manually install node-modules
- set them as global(which puts them in the shell's
PATH) usingnpm install -g <name> - install certain versions described by git tags
- install from a git url
- force a reinstall with
--force
- 手动安装节点模块
- 它们设置为全局(这使他们在外壳的
PATH使用)npm install -g <name> - 安装 git 标签描述的某些版本
- 从 git url 安装
- 强制重新安装
--force
回答by saeed
npm installinstalls all modules that are listed on package.jsonfile and their dependencies.
npm install安装package.json文件中列出的所有模块及其依赖项。
npm updateupdates all packages in the node_modulesdirectory and their dependencies.
npm update更新node_modules目录中的所有包及其依赖项。
npm install expressinstalls only the express module and its dependencies.
npm install express仅安装 express 模块及其依赖项。
npm update expressupdates express module (starting with [email protected], it doesn't update its dependencies).
npm update express更新 express 模块(从 [email protected] 开始,它不更新其依赖项)。
So updates are for when you already have the module and wish to get the new version.
因此,更新适用于您已经拥有该模块并希望获得新版本的情况。
回答by jmav
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
回答by MvG
Many distinctions have already been mentioned. Here is one more:
已经提到了许多区别。这里还有一个:
Running npm installat the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm installmay do considerably more work than just installing dependencies.
运行npm install在源代码目录的顶部将运行各种脚本:prepublish,preinstall,install,postinstall。根据这些脚本的作用,anpm install可能会做比仅安装依赖项更多的工作。
I've just had a use case where prepublishwould call makeand the Makefilewas designed to fetch dependencies if the package.jsongot updated. Calling npm installfrom within the Makefilewould have lead to an infinite recursion, while calling npm updateworked just fine, installing all dependencies so that the build could proceed even if makewas called directly.
我刚刚有一个用例,它prepublish会调用,make并且Makefile旨在package.json在更新时获取依赖项。npm install从内部调用Makefile将导致无限递归,而调用npm update工作得很好,安装所有依赖项,以便即使make直接调用构建也可以继续。
回答by DSK
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 中定义的节点模块(无需更新)

