1
0
wiki/Professional/Work/full-stack/工具/包管理工具.html

466 lines
479 KiB
HTML
Raw Normal View History

2024-09-05 22:26:11 +08:00
<!DOCTYPE html>
<html><head><title>包管理器</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="包管理器"/><meta property="og:description" content="JavaScript 包管理工具."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="JavaScript 包管理工具."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
(function() {
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-MHMEL0F832');
})();
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Schibsted Grotesk:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Professional/Work/full-stack/工具/包管理工具"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447-14.65,22.008-14.65,
<p><strong><a href="https://www.npmjs.com/" class="external">npm</a> 是 JavaScript 编写的软件包管理工具,同时也是 node.js 的默认包管理工具。</strong></p>
<h3 id="早期版本">早期版本<a aria-hidden="true" tabindex="-1" href="#早期版本" class="internal"> §</a></h3>
<p>因为早期 npm 版本采用<strong>嵌套结构</strong>,所以存在依赖地狱和存储空间占用过大问题;同时 Windows 环境下路径限制 256 字符,可能会导致运行出错。</p>
<p><img src="https://static.7wate.com/img/2022/09/06/e22a7d7484ab5.png" alt="node_modules"/></p>
<h3 id="v3-版本">V3 版本<a aria-hidden="true" tabindex="-1" href="#v3-版本" class="internal"> §</a></h3>
<p>V3 版本采用<strong>扁平结构</strong>来避免过深的依赖树和包冗余,子依赖会<strong>尽量平铺在主依赖所在的目录</strong>中(同一包但版本号不同还是会放至子目录中)。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">└── C@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── B@2.0.0</span></span></code></pre></div>
<p>虽然解决了依赖地狱的问题,但是又形成了新的问题。</p>
<h4 id="幽灵依赖">幽灵依赖<a aria-hidden="true" tabindex="-1" href="#幽灵依赖" class="internal"> §</a></h4>
<p>幽灵依赖是指在 package.json 中未定义的依赖,但项目中依然可以正确地被引用到。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">{</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> &quot;dependencies&quot;: {</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> &quot;A&quot;: &quot;^1.0.0&quot;,</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> &quot;C&quot;: &quot;^1.0.0&quot;</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> }</span></span>
<span data-line><span style="color:var(--shiki-color-text);">}</span></span></code></pre></div>
<p>例如我们只安装了 A 和 C虽然 A 引用了 B但是因为平铺在同一目录所以在项目中引用 B 还是可以正常工作的。将来如果某个版本的 A 依赖不再依赖 B 或者 B 的版本发生了变化,那么就会造成<strong>依赖缺失</strong>或兼容性问题。</p>
<h4 id="不确定性">不确定性<a aria-hidden="true" tabindex="-1" href="#不确定性" class="internal"> §</a></h4>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">└── C@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── B@2.0.0</span></span></code></pre></div>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── B@2.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">└── C@1.0.0</span></span></code></pre></div>
<p>不确定性是指同样的 package.json 文件install 依赖后可能不会得到同样的 node_modules 目录结构。如果 A 依赖 B@1.0C 依赖 B@2.0;依赖安装后究竟该提升 B 为 1.0 还是 2.0 取决于用户的安装顺序。</p>
<h4 id="依赖分身">依赖分身<a aria-hidden="true" tabindex="-1" href="#依赖分身" class="internal"> §</a></h4>
<p>假设继续再安装依赖 B@1.0 的 D 模块和依赖 @B2.0 的 E 模块此时A 和 D 依赖 B@1.0C 和 E 依赖 B@2.0。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── D@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── C@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── B@2.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">└── E@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> └── B@2.0.0</span></span></code></pre></div>
<p>可以看到 B@2.0 会被安装两次,实际上无论提升 B@1.0 还是 B@2.0,都会存在重复版本的 B 被安装,这两个重复安装的 B 就叫依赖分身。</p>
<h3 id="安装方式">安装方式<a aria-hidden="true" tabindex="-1" href="#安装方式" class="internal"> §</a></h3>
<p>可以通过 <a href="https://nodejs.org/zh-cn/" class="external">node.js 官网</a> 下载安装,或者直接使用如下脚本。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">curl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-qL</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">https://www.npmjs.com/install.sh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">sh</span></span></code></pre></div>
<h3 id="常用命令">常用命令<a aria-hidden="true" tabindex="-1" href="#常用命令" class="internal"> §</a></h3>
<table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>npm help</td><td>帮助文档</td></tr><tr><td>npm -v</td><td>npm 版本</td></tr><tr><td>npm config list -l</td><td>npm 配置</td></tr><tr><td>npm init</td><td>初始化引导创建 package.json 文件</td></tr><tr><td>npm set &lt;变量名> &lt;值></td><td>设置环境变量</td></tr><tr><td>npm search &lt;关键词> [-g]</td><td>检索模块</td></tr><tr><td>npm list</td><td>局部查看模块</td></tr><tr><td>npm list -g —depth 0</td><td>全局安装的模块,目录深度为 0。</td></tr><tr><td>npm install</td><td>读取 package.json 安装模块</td></tr><tr><td>npm uninstall &lt;模块> [-g]</td><td>卸载局部模块或全局模块</td></tr><tr><td>npm update &lt;模块> [-g]</td><td>升级局部模块或全局的指定模块</td></tr><tr><td>npm run &lt;脚本></td><td>运行 package.json 脚本</td></tr></tbody></table>
<h2 id="yarn">Yarn<a aria-hidden="true" tabindex="-1" href="#yarn" class="internal"> §</a></h2>
<p><strong><a href="https://yarnpkg.com/" class="external">yarn</a> 同样采用扁平结构,它的出现是为了解决 npm V3 依赖安装速度慢和不确定性。</strong></p>
<h3 id="提升安装速度">提升安装速度<a aria-hidden="true" tabindex="-1" href="#提升安装速度" class="internal"> §</a></h3>
<p>yarn 采用<strong>并行模式</strong>替代 npm <strong>串行模式</strong>安装包,并且利用全局缓存可以提升较大安装速度。</p>
<h3 id="lockfile-解决不确定性">Lockfile 解决不确定性<a aria-hidden="true" tabindex="-1" href="#lockfile-解决不确定性" class="internal"> §</a></h3>
<p>安装依赖时,根据 package.josn 生成一份 yarn.lock 文件。因为 lockfile 里记录了依赖,以及依赖的子依赖、依赖的版本、获取地址、验证模块完整性的 hash所以即使是不同的安装顺序相同的依赖关系在任何的环境和容器中都能得到统一的 node_modules 目录结构,保证了依赖安装的确定性。</p>
<p><em><strong>npm v5 才发布 package-lock.json。</strong></em></p>
<h3 id="存在的问题">存在的问题<a aria-hidden="true" tabindex="-1" href="#存在的问题" class="internal"> §</a></h3>
<p>因为 yarn 和 npm 一样是扁平结构的 node_modules ,所以并没有解决<strong>幽灵依赖</strong><strong>依赖分身</strong>问题。</p>
<h3 id="安装方式-1">安装方式<a aria-hidden="true" tabindex="-1" href="#安装方式-1" class="internal"> §</a></h3>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">npm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--global</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">yarn</span></span></code></pre></div>
<h3 id="常用命令-1">常用命令<a aria-hidden="true" tabindex="-1" href="#常用命令-1" class="internal"> §</a></h3>
<table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>yarn help</td><td>帮助文档</td></tr><tr><td>yarn init</td><td>初始化项目</td></tr><tr><td>yarn install</td><td>安装所有依赖项</td></tr><tr><td>yarn add [package]</td><td>安装指定依赖项</td></tr><tr><td>yarn add [package]@[version | tag]</td><td>安装指定版本依赖</td></tr><tr><td>yarn up [package]</td><td>升级指定依赖</td></tr><tr><td>yarn up [package]@[version | tag]</td><td>升级指定版本依赖</td></tr><tr><td>yarn remove [package]</td><td>删除指定依赖</td></tr></tbody></table>
<h2 id="pnpm">Pnpm<a aria-hidden="true" tabindex="-1" href="#pnpm" class="internal"> §</a></h2>
<p><img src="https://static.7wate.com/img/2022/09/06/962148e0d436e.jpg" alt="img"/></p>
<p><a href="https://pnpm.io/zh/" class="external">pnpm</a> 不同于 npm 和 yarn 使用的扁平结构,而是采用了<strong>内容寻址存储</strong>。pnpm 通过设置<strong>全局 store</strong>,然后在项目中通过使用<strong>硬链接与符号链接</strong>引用依赖。为了实现此功能node_modules 目录下会多出 .pnpm 目录,而且是非扁平结构。</p>
<ul>
<li><strong>硬链接 Hard link</strong>:硬链接可以理解为<strong>源文件的副本</strong></li>
<li><strong>符号链接 Symbolic link</strong>:符号链接(软连接)可以理解为<strong>快捷方式</strong></li>
</ul>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="markdown" data-theme="default"><code data-language="markdown" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">&lt;store>/xxx 开头的路径是硬链接,指向全局 store 中安装的依赖。其余的是符号链接,指向依赖的快捷方式。</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);">node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── .pnpm</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ ├── A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ ├── A => &lt;store>/A@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── B => ../../B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ ├── B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── B => &lt;store>/B@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ ├── B@2.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ │ └── B => &lt;store>/B@2.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── C@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── node_modules</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ ├── C => &lt;store>/C@1.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);">│ └── B => ../../B@2.0.0</span></span>
<span data-line><span style="color:var(--shiki-color-text);"></span></span>
<span data-line><span style="color:var(--shiki-color-text);">├── A => .pnpm/A@1.0.0/node_modules/A</span></span>
<span data-line><span style="color:var(--shiki-color-text);">└── C => .pnpm/C@1.0.0/node_modules/C</span></span></code></pre></div>
<h3 id="未来可期">未来可期<a aria-hidden="true" tabindex="-1" href="#未来可期" class="internal"> §</a></h3>
<p>pnpm 这套全新的机制设计地十分巧妙,不仅兼容 node 的依赖解析,同时也解决了如下问题:</p>
<ul>
<li>
<p>幽灵依赖:只有直接依赖会平铺在 node_modules 下,子依赖不会被提升,不会产生幽灵依赖。</p>
</li>
<li>
<p>依赖分身:相同的依赖只会在全局 store 中安装一次。项目中的都是源文件的副本,几乎不占用任何空间,没有了依赖分身。</p>
</li>
</ul>
<p>同时由于链接的优势pnpm 的安装速度在大多数场景比 npm 和 yarn 快 2 倍,同时也节省更多的磁盘空间。但也存在一些弊端:</p>
<ul>
<li>
<p>因为 pnpm 创建的 node_modules 依赖软链接,所以在不支持软链接的环境中无法使用 pnpm比如 Electron 应用。</p>
</li>
<li>
<p>因为依赖源文件是安装在 store 中,调试依赖或 patch-package 给依赖打补丁也不太方便,可能会影响其他项目。</p>
</li>
</ul>
<h3 id="安装方式-2">安装方式<a aria-hidden="true" tabindex="-1" href="#安装方式-2" class="internal"> §</a></h3>
<h4 id="npm-1">Npm<a aria-hidden="true" tabindex="-1" href="#npm-1" class="internal"> §</a></h4>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">npm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-g</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pnpm</span></span></code></pre></div>
<h4 id="curl">Curl<a aria-hidden="true" tabindex="-1" href="#curl" class="internal"> §</a></h4>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">curl</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-fsSL</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">https://get.pnpm.io/install.sh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">sh</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-</span></span></code></pre></div>
<h3 id="常用命令-2">常用命令<a aria-hidden="true" tabindex="-1" href="#常用命令-2" class="internal"> §</a></h3>
<table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td><code>pnpm install</code></td><td>安装 package.json 文件中的所有依赖项</td></tr><tr><td><code>pnpm add &lt;pkg></code></td><td>安装指定的依赖项</td></tr><tr><td><code>pnpm update</code></td><td>更新所有的依赖项</td></tr><tr><td><code>pnpm uninstall</code></td><td>移除指定的依赖项</td></tr><tr><td><code>pnpm list</code></td><td>列出已安装的所有依赖项</td></tr><tr><td><code>pnpm run</code></td><td>运行在 package.json 文件中定义的脚本</td></tr><tr><td><code>pnpm test</code></td><td>运行测试</td></tr><tr><td><code>pnpm link</code></td><td>创建或者删除一个软链接到全局安装的依赖项</td></tr><tr><td><code>pnpm prune</code></td><td>移除无用的依赖项</td></tr><tr><td><code>pnpm publish</code></td><td>将你的包发布到 npm 注册表</td></tr><tr><td><code>pnpm root</code></td><td>打印全局安装的依赖项的位置</td></tr><tr><td><code>pnpm store</code></td><td>控制共享的包存储</td></tr><tr><td><code>pnpm outdated</code></td><td>检查哪些依赖项有新版本可以更新</td></tr><tr><td><code>pnpm rebuild</code></td><td>重新编译包</td></tr><tr><td><code>pnpm import</code></td><td>从 npm 转换一个项目</td></tr><tr><td><code>pnpm fetch</code></td><td>预下载所有从注册表下载的依赖项到本地的存储库</td></tr><tr><td><code>pnpm audit</code></td><td>检查项目依赖项中的已知的漏洞</td></tr></tbody></table>
<h2 id="npmyarnpnpm-功能比较">npmyarnpnpm 功能比较<a aria-hidden="true" tabindex="-1" href="#npmyarnpnpm-功能比较" class="internal"> §</a></h2>
<table><thead><tr><th>功能</th><th>pnpm</th><th>Yarn</th><th>npm</th></tr></thead><tbody><tr><td>工作空间支持monorepo</td><td>✔️</td><td>✔️</td><td>✔️</td></tr><tr><td>隔离的 <code>node_modules</code></td><td>✔️ - 默认</td><td>✔️</td><td></td></tr><tr><td>提升的 <code>node_modules</code></td><td>✔️</td><td>✔️</td><td>✔️ - 默认</td></tr><tr><td>自动安装 peers</td><td>✔️ - 通过 <a href="https://pnpm.io/zh/npmrc#auto-install-peers" class="external">auto-install-peers=true</a></td><td></td><td>✔️</td></tr><tr><td>PlugnPlay</td><td>✔️</td><td>✔️ - 默认</td><td></td></tr><tr><td>零安装</td><td></td><td>✔️</td><td></td></tr><tr><td>修补依赖项</td><td>✔️</td><td>✔️</td><td></td></tr><tr><td>管理 Node.js 版本</td><td>✔️</td><td></td><td></td></tr><tr><td>有锁文件</td><td>✔️ - <code>pnpm-lock.yaml</code></td><td>✔️ - <code>yarn.lock</code></td><td>✔️ - <code>package-lock.json</code></td></tr><tr><td>支持覆盖</td><td>✔️</td><td>✔️ - 通过 resolutions</td><td>✔️</td></tr><tr><td>内容可寻址存储</td><td>✔️</td><td></td><td></td></tr><tr><td>动态包执行</td><td>✔️ - 通过 <code>pnpm dlx</code></td><td>✔️ - 通过 <code>yarn dlx</code></td><td>✔️ - 通过 <code>npx</code></td></tr><tr><td>Side-effects cache</td><td>✔️</td><td></td><td></td></tr></tbody></table></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#npm" data-for="npm">Npm</a></li><li class="depth-1"><a href="#早期版本" data-for="早期版本">早期版本</a></li><li class="depth-1"><a href="#v3-版本" data-for="v3-版本">V3 版本</a></li><li class="depth-1"><a href="#安装方式" data-for="安装方式">安装方式</a></li><li class="depth-1"><a href="#常用命令" data-for="常用命令">常用命令</a></li><li class="depth-0"><a href="#yarn" data-for="yarn">Yarn</a></li><li class="depth-1"><a href="#提升安装速度" data-for="提升安装速度">提升安装速度</a></li><li class="depth-1"><a href="#lockfile-解决不确定性" data-for="lockfile-解决不确定性">Lockfile 解决不确定性</a></li><li class="depth-1"><a href="#存在的问题" data-for="存在的问题">存在的问题</a></li><li class="depth-1"><a href="#安装方式-1" data-for="安装方式-1">安装方式</a></li><li class="depth-1"><a href="#常用命令-1" data-for="常用命令-1">常用命令</a></li><li class="depth-0"><a href="#pnpm" data-for="pnpm">Pnpm</a></li><li class="depth-1"><a href="#未来可期" data-for="未来可期">未来可期</a></li><li class="depth-1"><a href="#安装方式-2" data-for="安装方式-2">安装方式</a></li><li class="depth-1"><a href="#常用命令-2" data-for="常用命令-2">常用命令</a></li><li class="depth-0"><a href="#npmyarnpnpm-功能比较" data-for="npmyarnpnpm-功能比较">npmyarnpnpm 功能比较</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Obsidian&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Obsidian/Templates&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2023&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2024&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/个人成长&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/医学健康&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/历史&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/哲学宗教&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/心理&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/政治军事&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/教育学习&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/文学&quot;,&quot;coll
function toggleCallout() {
const outerBlock = this.parentElement;
outerBlock.classList.toggle(`is-collapsed`);
const collapsed = outerBlock.classList.contains(`is-collapsed`);
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
outerBlock.style.maxHeight = height + `px`;
let current = outerBlock;
let parent = outerBlock.parentElement;
while (parent) {
if (!parent.classList.contains(`callout`)) {
return;
}
const collapsed2 = parent.classList.contains(`is-collapsed`);
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
parent.style.maxHeight = height2 + `px`;
current = parent;
parent = parent.parentElement;
}
}
function setupCallout() {
const collapsible = document.getElementsByClassName(
`callout is-collapsible`
);
for (const div of collapsible) {
const title = div.firstElementChild;
if (title) {
title.removeEventListener(`click`, toggleCallout);
title.addEventListener(`click`, toggleCallout);
const collapsed = div.classList.contains(`is-collapsed`);
const height = collapsed ? title.scrollHeight : div.scrollHeight;
div.style.maxHeight = height + `px`;
}
}
}
document.addEventListener(`nav`, setupCallout);
window.addEventListener(`resize`, setupCallout);
</script><script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
});
document.addEventListener('nav', async () => {
await mermaid.run({
querySelector: '.mermaid'
})
});
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>