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常用命令
- 查看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>
- 查看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>
- 查看安裝過的所有包
<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>
- 本地安裝依賴包和全局安裝(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>
- 卸載依賴包
<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>
- 安裝生產(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>
- 安裝開發(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>
- 安裝可選階段的依賴包
-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>
- 精確安裝指定版本依賴包
-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>
注意:
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
- 更新依賴包
<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>
- 檢查哪些依賴包是否過時(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)如下情況:
另外有個(gè)檢查依賴包更強(qiáng)大的一個(gè)第三方工具 npm-check失息,具體使用方法,請(qǐng)查看它的文檔。
- 查看和管理
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>
- 清除本地
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>
- 初始化項(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
支持可以使用支持npm
除publish
之外的所有命令故黑。
因?yàn)?code>cnpm的倉庫只是 npm
倉庫的一個(gè)拷貝,它不承擔(dān) publish
工作庭砍,所以執(zhí)行cnpm publish
命令會(huì)失敗的场晶。
yarn是什么?
yarn是個(gè)包管理器怠缸。你可以通過它使用全世界開發(fā)者的代碼诗轻, 或者分享自己的代碼。
從 npm
安裝軟件包并保持相同的包管理流程揭北。
- 特點(diǎn):
- 速度超快扳炬。
Yarn 緩存了每個(gè)下載過的包,所以再次使用時(shí)無需重復(fù)下載搔体。 同時(shí)利用并行下載以最大化資源利用率恨樟,因此安裝速度更快。
- 超級(jí)安全
在執(zhí)行代碼之前疚俱,Yarn 會(huì)通過算法校驗(yàn)每個(gè)安裝包的完整性劝术。
- 超級(jí)可靠
使用詳細(xì)、簡(jiǎn)潔的鎖文件格式和明確的安裝算法呆奕,Yarn 能夠保證在不同系統(tǒng)上無差異的工作夯尽。
- 安裝
- 通過命令安裝
<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
- 常用命令:
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