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è)打交道,下面簡(jiǎn)單的說說它們?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)物隔缀,簡(jiǎ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)前安裝過的所有包

或者簡(jiǎn)寫

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是什么邻梆?

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

這是一個(gè)完整 npmjs.org 鏡像校辩,你可以用此代替官方版本(只讀),同步頻率目前為 10分鐘 一次以保證盡量與官方服務(wù)同步辆童∫酥洌—來自淘寶 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ì)、簡(jiǎn)潔的鎖文件格式和明確的安裝算法呆奕,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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子麦射,更是在濱河造成了極大的恐慌蛾娶,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件潜秋,死亡現(xiàn)場(chǎng)離奇詭異蛔琅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)峻呛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門罗售,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人钩述,你說我怎么就攤上這事寨躁。” “怎么了牙勘?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵职恳,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我方面,道長(zhǎng)放钦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任恭金,我火速辦了婚禮操禀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蔚叨。我一直安慰自己床蜘,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布蔑水。 她就那樣靜靜地躺著邢锯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪搀别。 梳的紋絲不亂的頭發(fā)上丹擎,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音歇父,去河邊找鬼蒂培。 笑死,一個(gè)胖子當(dāng)著我的面吹牛榜苫,可吹牛的內(nèi)容都是我干的护戳。 我是一名探鬼主播,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼垂睬,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼媳荒!你這毒婦竟也來了抗悍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤钳枕,失蹤者是張志新(化名)和其女友劉穎缴渊,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鱼炒,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衔沼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了昔瞧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片指蚁。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖硬爆,靈堂內(nèi)的尸體忽然破棺而出欣舵,到底是詐尸還是另有隱情,我是刑警寧澤缀磕,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布缘圈,位于F島的核電站,受9級(jí)特大地震影響袜蚕,放射性物質(zhì)發(fā)生泄漏糟把。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一牲剃、第九天 我趴在偏房一處隱蔽的房頂上張望遣疯。 院中可真熱鬧,春花似錦凿傅、人聲如沸缠犀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辨液。三九已至,卻和暖如春箱残,著一層夾襖步出監(jiān)牢的瞬間滔迈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來泰國打工被辑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留燎悍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓盼理,卻偏偏與公主長(zhǎng)得像谈山,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宏怔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容