When you install a package using npm install <packagename>
, the latest available version of the package is downloaded and put in the node_modules
folder, and a corresponding entry is added to the package.json
and package-lock.json
files that are present in your current folder.
使用npm install <packagename>
软件包时,将下载该软件包的最新可用版本并将其放在node_modules
文件夹中,并将相应的条目添加到其中存在的package.json
和package-lock.json
文件中。您当前的文件夹。
npm calculates the dependencies and installs the latest available version of those as well.
npm计算依赖关系,并安装这些依赖关系的最新可用版本。
Let’s say you install cowsay
, a cool command line tool that lets you make a cow say things.
比方说,你安装cowsay
,一个很酷的命令行工具,可以让用户一头牛说的事情 。
When you npm install cowsay
, this entry is added to the package.json
file:
当您npm install cowsay
,此条目将添加到package.json
文件中:
{
"dependencies": {
"cowsay": "^1.3.1"
}
}
and this is an extract of package-lock.json
, where I removed the nested dependencies for clarity:
这是package-lock.json
,为清楚起见,我在其中删除了嵌套的依赖项:
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"cowsay": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/cowsay/-/cowsay-1.3.1.tgz",
"integrity": "sha512-3PVFe6FePVtPj1HTeLin9v8WyLl+VmM1l1H/5P+BTTDkMAjufp+0F9eLjzRnOHzVAYeIYFF5po5NjRrgefnRMQ==",
"requires": {
"get-stdin": "^5.0.1",
"optimist": "~0.6.1",
"string-width": "~2.1.1",
"strip-eof": "^1.0.0"
}
}
}
}
Now those 2 files tell us that we installed version 1.3.1
of cowsay, and our rule for updates is ^1.3.1
, which for the npm versioning rules means that npm can update to patch and minor releases: 1.3.2
, 1.4.0
and so on.
现在,这两个文件告诉我们,我们安装了Cowsay的1.3.1
版本,更新规则是^1.3.1
,这对于npm版本控制规则意味着npm可以更新到补丁程序和次要发行版: 1.3.2
和1.4.0
,依此类推。
But not for major version changes that break compatibility, which means, in this example, 2.0
and higher.
但不适用于破坏兼容性的主要版本更改,在此示例中,这意味着2.0
及更高版本。
If there is a new minor or patch release and we type npm update
, the installed version is updated, and the package-lock.json
file diligently filled with the new version.
如果有新的次要版本或补丁程序发布,并且我们键入npm update
,那么将npm update
已安装的版本,并且package-lock.json
文件会努力地用新版本填充。
package.json
remains unchanged.
package.json
保持不变。
To discover new releases of the packages, you run npm outdated
.
要发现软件包的新版本,请运行npm outdated
。
Here’s the list of a few outdated packages in one repository I didn’t update for quite a while:
以下是一个存储库中一些过时的软件包的列表,我已经有一段时间没有更新了:
Some of those updates are major releases. Running npm update
won’t update the version of those. Major releases are never updated in this way because they (by definition) introduce breaking changes, and npm
want to save you trouble.
其中一些更新是主要版本。 运行npm update
不会更新那些版本。 绝不会以这种方式更新主要版本,因为它们(根据定义)会引入重大更改, npm
希望为您省去麻烦。
To update to a new major version all the packages, install the npm-check-updates
package globally:
要将所有软件包更新到新的主要版本,请全局安装npm-check-updates
软件包:
npm install -g npm-check-updates
then run it:
然后运行它:
ncu -u
this will upgrade all the version hints in the package.json
file, to dependencies
and devDependencies
, so npm can install the new major version.
这会将package.json
文件中的所有版本提示升级为dependencies
和devDependencies
,因此npm可以安装新的主要版本。
You are now ready to run the update:
现在,您可以运行更新了:
npm update
If you just downloaded the project without the node_modules
dependencies and you want to install the shiny new versions first, just run
如果您只是下载了没有node_modules
依赖项的项目,并且想先安装闪亮的新版本,请运行
npm install