toFrankie / blog

种一棵树,最好的时间是十年前。其次,是现在。
20 stars 1 forks source link

npm install 指定版本 #309

Open toFrankie opened 1 year ago

toFrankie commented 1 year ago

配图源自 Freepik

npm 安装包的方式很多很多,本文就以下几种方式作简要介绍。

$ npm install <name>
$ npm install <name>@<tag>
$ npm install <name>@<version>
$ npm install <name>@<version range>

npm install \

它其实就是 npm install <name>@<tag> 简写形式。由于 <tag> 默认为 latest(最新版本),因此在不作任何配置的情况下 npm install <name> 会按照该包的最新版本。

npm install \name>@\<tag

tag 是什么?

在软件开发的过程中,如有重大的版本变更,我们通常会给它打上一个「标签」,它只是一个更友好、易于理解的「别名」罢了。像平常使用 Git 做版本管理时,也会用 git tag 打标签。

有哪些 tag?

默认情况下,latest 标签用来标识某个包的最新版本。

除了 latest 之外,没有任何标签对 npm 本身有任何特殊意义。

一个包如有多个开发流程可以自定义一些有意义的 tag,比如:stablealphabetadevcanarynext。除了 latest 之外的所有 tag,都需要在手动指定。

如何查看 tag?

通过 npm-dist-tag 命令:

$ npm dist-tag ls <name>

举个例子:

$ npm dist-tag ls lodash-es
latest: 4.17.21

$ npm dist-tag ls react
beta: 18.0.0-beta-24dd07bd2-20211208
experimental: 0.0.0-experimental-b14f8da15-20230403
latest: 18.2.0
next: 18.3.0-next-b14f8da15-20230403
rc: 18.0.0-rc.3

或在 npmjs.com 等平台查看:

如何添加/移除 tag?

在发包时通过 --tag 参数指定,若无指定,则默认为 latest。比如:

$ npm publish --tag <tag>

对于已发布的版本,可以通过 npm dist-tag addnpm dist-tag rm 来添加/移除 tag。比如:

$ npm dist-tag add <name>@<version> <tag>
$ npm dist-tag rm <name> <tag>

举个例子:

# 未指定时,默认为 latest
$ npm publish
npm notice 
npm notice 📦  @summer/my-pack@0.0.1
npm notice === Tarball Contents === 
npm notice 27B  README.md   
npm notice 232B package.json
npm notice 27B  src/index.js
npm notice === Tarball Details === 
npm notice name:          @summer/my-pack                         
npm notice version:       0.0.1                                   
npm notice filename:      summer-my-pack-0.0.1.tgz                
npm notice package size:  342 B                                   
npm notice unpacked size: 286 B                                   
npm notice shasum:        84c45d5eb997b714cb1348624b71046ed6c987a8
npm notice integrity:     sha512-blbi1Q3A8La3A[...]r6BVjGtYZ8rCw==
npm notice total files:   3                                       
npm notice 
npm notice Publishing to http://localhost:4873/ with tag latest and default access
+ @summer/my-pack@0.0.1

$ npm dist-tag ls @summer/my-pack
latest: 0.0.1

# 发布一个名为 next 的 tag
$ npm publish --tag next
npm notice 
npm notice 📦  @summer/my-pack@0.0.2
npm notice === Tarball Contents === 
npm notice 27B  README.md   
npm notice 232B package.json
npm notice 27B  src/index.js
npm notice === Tarball Details === 
npm notice name:          @summer/my-pack                         
npm notice version:       0.0.2                                   
npm notice filename:      summer-my-pack-0.0.2.tgz                
npm notice package size:  342 B                                   
npm notice unpacked size: 286 B                                   
npm notice shasum:        e9575fc34aacf2a08bb4f33a768dcf5ba4aab494
npm notice integrity:     sha512-kcHYodhWGbAds[...]TU53bhFy46wUw==
npm notice total files:   3                                       
npm notice 
npm notice Publishing to http://localhost:4873/ with tag next and default access
+ @summer/my-pack@0.0.2

$ npm dist-tag ls @summer/my-pack
latest: 0.0.1
next: 0.0.2

# 移除 next tag,但注意移除后 latest 仍为 0.0.1 版本
$ npm dist-tag rm @summer/my-pack next
-next: @summer/my-pack@0.0.2

$ npm dist-tag ls @summer/my-pack
latest: 0.0.1

# 手动指定 0.0.2 版本为 latest tag
$ npm dist-tag add @summer/my-pack@0.0.2 latest
+latest: @summer/my-pack@0.0.2

# 手动指定 0.0.1 版本为 legacy tag
$ npm dist-tag add @summer/my-pack@0.0.1 legacy
+legacy: @summer/my-pack@0.0.1

$ npm dist-tag ls @summer/my-pack
latest: 0.0.2
legacy: 0.0.1

安装指定 tag

前面了解 tag 之后,安装就很容易理解了。

$ npm install <name>@<tag>

# 相当于
$ npm install <name> --tag <tag>

如有特殊需要,可通过 npm config set tag <tag> 去配置 tag(详见),后面 npm install 不指定 tag 时,默认取该配置值。

npm install \name>@\<version

安装时指定版本或指定版本范围:

$ npm install <name>@<version>
$ npm install <name>@<version range>

安装确切版本

一是安装时指定确切版本,二是配置 save-exacttruenpm config set save-exact true)。比如:

$ npm install react@18.0.0

# 相当于(假设当前 react 的 latest 为 18.0.0)
$ npm config set save-exact true
$ npm install react

那么安装就不会下载符合 ^x.y.z~x.y.z 范围的版本了。

安装 ^ 或 ~ 版本

使用 npm install <name> 安装包时,它会以 ^x.y.z 形式添加到 package.json 里面。因为 npm 的 save-prefix 默认配置就是 ^,可通过 npm config set save-prefix '~' 指定为 ~。二者含义,大家都懂就不再展开赘述了。

安装主要版本的最新版本

$ npm install <name>@<major-version>

比如 npm install react@16npm install react@16.x 会安装 16.x 中最新的版本 ^16.14.0

最近也遇到这种需求:目前 ora 最新版本是 6.3.0,该版本不再支持 CommonJS 形式导入,因此使用 const ora = require('ora') 将会报错:

const ora = require('ora')
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/frankie/Web/Temp/demo/node_modules/ora/index.js from /Users/frankie/Web/Temp/demo/src/index.js not supported.

可安装 5.x 版本以支持 CommonJS(npm install ora@5)。

npm install \@\

范围可通过使用 ><=- 等限定符组合。比如:

# 安装范围内的最新版本(17.0.1)
$ npm install react@">=16.0.0 <17.0.2" 

# 安装大版本为 16 至 17 的最新版本(17.0.2)
$ npm install react@"16 - 17"

References