<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Andrei Chernykh</title>
    <description>The latest articles on Forem by Andrei Chernykh (@andreychernykh).</description>
    <link>https://forem.com/andreychernykh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F513407%2F32cb95bf-14df-4930-adc0-d07543e28f1f.jpeg</url>
      <title>Forem: Andrei Chernykh</title>
      <link>https://forem.com/andreychernykh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/andreychernykh"/>
    <language>en</language>
    <item>
      <title>How to migrate from yarn / npm to pnpm</title>
      <dc:creator>Andrei Chernykh</dc:creator>
      <pubDate>Sun, 29 May 2022 20:17:30 +0000</pubDate>
      <link>https://forem.com/andreychernykh/yarn-npm-to-pnpm-migration-guide-2n04</link>
      <guid>https://forem.com/andreychernykh/yarn-npm-to-pnpm-migration-guide-2n04</guid>
      <description>&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;pnpm&lt;/code&gt; is more performant at fetching, resolving, and storing dependencies. My personal experience shows that in some projects &lt;code&gt;pnpm&lt;/code&gt; can be approx. 10x time faster at resolving dependencies and up to 3x more efficient for disk usage.&lt;/p&gt;

&lt;p&gt;It is also easy to start using &lt;code&gt;pnpm&lt;/code&gt; if you have used &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt; before because the CLI is very similar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pnpm.io/motivation"&gt;https://pnpm.io/motivation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration guide
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Install &lt;code&gt;pnpm&lt;/code&gt; &lt;a href="https://pnpm.io/installation"&gt;Installation&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Delete &lt;code&gt;node_modules&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx npkill
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Add to &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"preinstall"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npx only-allow pnpm"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prevent other devs from accidentally installing dependencies with anything else than &lt;code&gt;pnpm&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;: Create &lt;code&gt;pnpm-workspace.yaml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;packages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# include packages in subfolders (e.g. apps/ and packages/)&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apps/**"&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;packages/**'&lt;/span&gt;
  &lt;span class="c1"&gt;# if required, exclude some directories&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;!**/test/**'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4 (a)&lt;/strong&gt;: remove &lt;code&gt;"workspaces"&lt;/code&gt; from &lt;code&gt;"package.json"&lt;/code&gt;, since it's no longer needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;: Run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm import
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a &lt;code&gt;pnpm-lock.yaml&lt;/code&gt; file based on &lt;code&gt;yarn.lock&lt;/code&gt; (or &lt;code&gt;packages-lock.json&lt;/code&gt;)&lt;br&gt;
&lt;strong&gt;Step 6&lt;/strong&gt;: Remove &lt;code&gt;yarn.lock&lt;/code&gt; (or &lt;code&gt;packages-lock.json&lt;/code&gt;)&lt;br&gt;
&lt;strong&gt;Step 7&lt;/strong&gt;: Install dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pnpm i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 8&lt;/strong&gt;: Replace &lt;code&gt;npm run&lt;/code&gt; (or &lt;code&gt;yarn&lt;/code&gt;) to &lt;code&gt;pnpm&lt;/code&gt; in all &lt;code&gt;package.json&lt;/code&gt; and other files (E.g. &lt;code&gt;pnpm test&lt;/code&gt; instead of &lt;code&gt;npm run test&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important! You need to keep in mind that &lt;code&gt;pnpm&lt;/code&gt; doesn’t use dependency hoisting:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When installing dependencies with npm or Yarn Classic, all packages are hoisted to the root of the modules directory. As a result, source code has access to dependencies that are not added as dependencies to the project.&lt;br&gt;
By default, pnpm uses symlinks to add only the direct dependencies of the project into the root of the modules directory. &lt;br&gt;
&lt;a href="https://pnpm.io/motivation#creating-a-non-flat-node_modules-directory"&gt;pnpm&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In practice it means that if you have a package &lt;code&gt;A&lt;/code&gt; that imports a package &lt;code&gt;B&lt;/code&gt;  (&lt;code&gt;import something from 'B'&lt;/code&gt;) but doesn’t explicitly specify &lt;code&gt;B&lt;/code&gt; in the &lt;code&gt;dependencies&lt;/code&gt; or &lt;code&gt;devDependencies&lt;/code&gt;, then the execution will fail.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Cheatsheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tables&lt;/th&gt;
&lt;th&gt;Commands&lt;/th&gt;
&lt;th&gt;Cool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Install dependencies&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/cli/install"&gt;https://pnpm.io/cli/install&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add a dependency&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm add &amp;lt;package&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/cli/add"&gt;https://pnpm.io/cli/add&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shows all packages that depend on the specified package&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm why &amp;lt;package&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/cli/why"&gt;https://pnpm.io/cli/why&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Run a command as if it was executed form the root of the project rather than a workspace package&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm -w &amp;lt;command&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/pnpm-cli#-w---workspace-root"&gt;https://pnpm.io/pnpm-cli#-w---workspace-root&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Restrict commands to specific subsets of packages&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm --filter &amp;lt;package_selector&amp;gt; &amp;lt;command&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/filtering"&gt;https://pnpm.io/filtering&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;This runs an arbitrary command from each package's "scripts" object&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pnpm -r &amp;lt;command&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href="https://pnpm.io/cli/run#--recursive--r"&gt;https://pnpm.io/cli/run#--recursive--r&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>pnpm</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
