npm、cnpm、yarn三劍客

npm,cnpm,yarn這三個(gè)或許使用Hexo,Hugo,vuepress等靜態(tài)博客搭建的小伙伴們來說,會(huì)經(jīng)常遇見,會(huì)使用它們進(jìn)行插件,依賴包等的安裝、卸載與更新,如果你是前端開發(fā)者或者是準(zhǔn)備從事前端的話,你會(huì)經(jīng)常與它們?nèi)齻€(gè)打交道,下面簡單的說說它們?nèi)齽桶桑?/p>

npm是什么?

npm 為你和你的團(tuán)隊(duì)打開了連接整個(gè) JavaScript 天才世界的一扇大門。它是世界上最大的軟件注冊(cè)表,每星期大約有 30 億次的下載量,包含超過 600000 個(gè) 包(package) (即,代碼模塊)。來自各大洲的開源軟件開發(fā)者使用 npm 互相分享和借鑒。包的結(jié)構(gòu)使您能夠輕松跟蹤依賴項(xiàng)和版本。引自npm中文網(wǎng)

npm (node package manager)是 JavaScript 世界的包管理工具,是 Node.js 平臺(tái)的默認(rèn)包管理工具,是前端模塊化下的一個(gè)標(biāo)志性產(chǎn)物,簡單地說,就是通過npm下載模塊,復(fù)用已有的代碼,提高工作效率。

下面是關(guān)于 npm 的快速介紹:

npm 由三個(gè)獨(dú)立的部分組成:

  • 網(wǎng)站

  • 注冊(cè)表(registry)

  • 命令行工具 (CLI)

網(wǎng)站 是開發(fā)者查找包(package)、設(shè)置參數(shù)以及管理 npm 使用體驗(yàn)的主要途徑。

注冊(cè)表 是一個(gè)巨大的數(shù)據(jù)庫,保存了每個(gè)包(package)的信息。

CLI 通過命令行或終端運(yùn)行。開發(fā)者通過 CLI 與 npm 打交道。

npm常用命令

  1. 查看npm的版本

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm -v #npm -version</pre>

  1. 查看npm幫助

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 查看所有的命令幫助
npm help

如果要單獨(dú)查看install命令的幫助,可以使用下面的命令

npm help install</pre>

  1. 查看安裝過的所有包

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm list #列出當(dāng)前安裝過的所有包

或者簡寫

npm ls
npm ls -g #查看全局安裝的模塊及依賴 </pre>

  1. 本地安裝依賴包和全局安裝(global)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#安裝nodejs的依賴包,默認(rèn)安裝最新版本(本地)
npm install <Package name>

一次性安裝多個(gè)(本地)

npm install 模塊名 模塊名

安裝指定版本號(hào)的依賴包(本地)

npm install <Package name>@version

將包安裝到全局環(huán)境中

npm install <Package name> -g

根據(jù)package.json文件安裝

npm install</pre>

  1. 卸載依賴包

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm uninstall <Package name>
npm uninstall gulp --save-dev #卸載開發(fā)版本的模塊</pre>

  1. 安裝生產(chǎn)階段的依賴包

-S, - -save 安裝包信息將加入到dependencies(生產(chǎn)階段的依賴)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install gulp --save 或 npm install gulp -S</pre>

package.json文件的 dependencies 字段:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="json" cid="n47" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"dependencies": {
"gulp": "^4.0.2"
}</pre>

  1. 安裝開發(fā)階段的依賴包

-D, --save-dev 安裝包信息將加入到devDependencies(開發(fā)階段的依賴),所以開發(fā)階段一般使用它

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n53" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install gulp --save-dev 或 npm install gulp -D</pre>

package.json 文件的devDependencies字段:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="json" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"devDependencies": {
"gulp": "^4.0.2"
}</pre>

  1. 安裝可選階段的依賴包

-O, --save-optional 安裝包信息將加入到optionalDependencies(可選階段的依賴)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n61" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install gulp --save-optional 或 npm install gulp -O</pre>

package.json文件的optionalDependencies字段:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="json" cid="n63" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"optionalDependencies": {
"gulp": "^4.0.2"
}</pre>

  1. 精確安裝指定版本依賴包

-E, --save-exact 精確安裝指定模塊版本

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n69" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install gulp --save-exact 或 npm install gulp -E</pre>

輸入命令npm install gulp -ES,留意package.json 文件的 dependencies字段,以看出版本號(hào)中的^**消失了。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="json" cid="n71" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"dependencies": {
"gulp": "4.0.2"
}</pre>

模塊的依賴都被寫入了package.json文件后,他人打開項(xiàng)目的根目錄(項(xiàng)目開源、內(nèi)部團(tuán)隊(duì)合作),使用npm install命令可以根據(jù)dependencies配置安裝所有的依賴包。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n73" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install</pre>

image

注意:

npm 的package.json中的~和^

  • ~ 會(huì)匹配最近的小版本依賴包,比如~1.2.3會(huì)匹配所有1.2.x版本,但是不包括1.3.0

  • ^ 會(huì)匹配最新的大版本依賴包,比如^1.2.3會(huì)匹配所有1.x.x的包,包括1.3.0,但是不包括2.0.0

  1. 更新依賴包

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n85" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm update <Package name></pre>

  1. 檢查哪些依賴包是否過時(shí)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n89" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm outdated</pre>

執(zhí)行上面的命令,會(huì)出現(xiàn)如下情況:

image

另外有個(gè)檢查依賴包更強(qiáng)大的一個(gè)第三方工具 npm-check,具體使用方法,請(qǐng)查看它的文檔。

  1. 查看和管理npm的基礎(chǔ)配置

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n96" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm config list -l #查看所有的配置</pre>

  1. 清除本地npm cache緩存

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm cache clean</pre>

  1. 初始化項(xiàng)目

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n104" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm init #初始化項(xiàng)目,并在項(xiàng)目文件夾中引導(dǎo)創(chuàng)建一個(gè)package.json文件</pre>

其他npm命令可以看NPM中文文檔

cnpm是什么?

簡單的講就是中國版的NPM,因?yàn)閚pm安裝插件是從國外服務(wù)器下載,受網(wǎng)絡(luò)影響大,可能出現(xiàn)異常,所以我們樂于分享的淘寶團(tuán)隊(duì)干了這事。

這是一個(gè)完整 npmjs.org 鏡像,你可以用此代替官方版本(只讀),同步頻率目前為 10分鐘 一次以保證盡量與官方服務(wù)同步?!獊碜?a target="_blank">淘寶 NPM 鏡像

如何使用cnpm?

首先就是需要安裝cnpm模塊,命令如下:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n112" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install -g cnpm --registry=https://registry.npm.taobao.org</pre>

cnpm支持可以使用支持npmpublish之外的所有命令。

因?yàn)?code>cnpm的倉庫只是 npm 倉庫的一個(gè)拷貝,它不承擔(dān) publish 工作,所以執(zhí)行cnpm publish 命令會(huì)失敗的。

yarn是什么?

yarn是個(gè)包管理器。你可以通過它使用全世界開發(fā)者的代碼, 或者分享自己的代碼。

npm 安裝軟件包并保持相同的包管理流程。

  1. 特點(diǎn):
  • 速度超快。

Yarn 緩存了每個(gè)下載過的包,所以再次使用時(shí)無需重復(fù)下載。 同時(shí)利用并行下載以最大化資源利用率,因此安裝速度更快。

  • 超級(jí)安全

在執(zhí)行代碼之前,Yarn 會(huì)通過算法校驗(yàn)每個(gè)安裝包的完整性。

  • 超級(jí)可靠

使用詳細(xì)、簡潔的鎖文件格式和明確的安裝算法,Yarn 能夠保證在不同系統(tǒng)上無差異的工作。

  1. 安裝
  • 通過命令安裝

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="bash" cid="n139" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Cousine, Consolas, monospace; font-size: 14px; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(30, 32, 34); position: relative !important; width: auto; color: rgb(221, 221, 221); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">npm install -g yarn</pre>

查看版本:yarn --version

  • 通過安裝包程序安裝

地址:https://classic.yarnpkg.com/zh-Hans/docs/install#windows-stable

  1. 常用命令:
  • yarn init:初始化項(xiàng)目,會(huì)在根目錄下生成一個(gè)package.json,與npm類似具體不做解釋

  • yarn add <Package name>:安裝依賴包,會(huì)自動(dòng)安裝最新版本,會(huì)覆蓋指定版本號(hào)

  • yarn add <Package name1> <Package name1>:一次性添加多個(gè)包

  • yarn add <Package name>@version:添加指定版本的包

  • yarn upgrade <Package name>@version:將包更新到指定版本

  • yarn upgrade --latest <Package name>:將包更新到最新版本

  • yarn remove <Package name>:刪除包

  • yarn remove <Package name1> <Package name2>:一次性刪除多個(gè)包

  • yarn info <Packagename> :可以用來查看某個(gè)模塊的最新版本信息

  • yarn install:安裝package.json里所有包,并將包及它的所有依賴項(xiàng)保存進(jìn)yarn.lock

  • yarn install --force :強(qiáng)制重新下載所有包

  • yarn install --flat:安裝一個(gè)包的單一版本

  • yarn install --production:只安裝dependencies里的包

  • yarn install --no-lockfile:不讀取或生成yarn.lock

  • yarn install --pure-lockfile:不生成yarn.lock

具體詳細(xì)的命令可以查看官方文檔:https://classic.yarnpkg.com/zh-Hans/docs

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容