<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ pnpm - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ pnpm - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 10:49:58 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/pnpm/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use pnpm – Installation and Common Commands ]]>
                </title>
                <description>
                    <![CDATA[ pnpm is like npm, but it's way faster and more efficient. After all, the starting p stands for _p_erformant. According to its creator, Zoltan Kochan, pnpm "allows you to save gigabytes of disk space." Many popular projects including Next.js, Vite, Sv... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-pnpm/</link>
                <guid isPermaLink="false">66c5f749bae1f0a10743aece</guid>
                
                    <category>
                        <![CDATA[ node js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ performance ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pnpm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ashutosh Biswas ]]>
                </dc:creator>
                <pubDate>Tue, 09 Jan 2024 15:31:52 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/cover-pnpm-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><em>pnpm</em> is like npm, but it's way faster and more efficient. After all, the starting <em>p</em> stands for _p_erformant.</p>
<p>According to its creator, Zoltan Kochan, pnpm "allows you to save gigabytes of disk space."</p>
<p>Many popular projects including Next.js, Vite, Svelte, and even freeCodeCamp use pnpm. So now is a great time try out this tool if you haven't yet. I'm sure your time will not be wasted.</p>
<p>In this article, I won't go into details of why pnpm is faster and more efficient than npm. You can check out the <a target="_blank" href="https://pnpm.io/motivation">official documentation</a> if you want to know more about that.</p>
<p>The goal of this article is to quickly get you started with pnpm so you can perform your day to day tasks that you previously did with npm or yarn. Grab your favorite cup of tea or coffee ☕️, and let's dive right in! 🚀</p>
<h2 id="heading-how-to-install-pnpm">How to Install pnpm</h2>
<p>I assume you already have a modern version of Node.js installed on your machine. These modern versions come with a command called <code>corepack</code>. It let's you manage your Node package managers. </p>
<p>Yes you read that right! It's an experimental feature of Node but it works pretty well. </p>
<p>So to start using it, you first need to enable it by entering the following command from your terminal, which has the effect of installing pnpm (and also yarn) on your system:</p>
<pre><code class="lang-zsh">corepack <span class="hljs-built_in">enable</span>
</code></pre>
<p>It's that simple. Now if you run <code>pnpm --version</code> you will see the version you have just installed. But this might not be the latest version of pnpm. If this is the case, you can install the latest version of pnpm using this command:</p>
<pre><code class="lang-zsh">corepack prepare pnpm@latest --activate
</code></pre>
<p>Keep in mind that there are many ways to install pnpm on your system, and you can read about all of them in the <a target="_blank" href="https://pnpm.io/installation">installation docs</a>. My favorite is the <code>corepack</code> formula I've shown above.</p>
<h2 id="heading-how-to-configure-your-shell-for-efficiency-optional">How to Configure your Shell for Efficiency (Optional)</h2>
<p>Well, you now have pnpm installed. But the default command line experience can be improved to save you some effort. </p>
<p>Note that this section is optional. If you want you can skip to the next section. But if you are serious about setting it up so that the CLI experience is pleasant, let's learn how to do it.</p>
<h3 id="heading-pnpm-is-hard-to-type-so-set-up-an-alias"><code>pnpm</code> is Hard to Type – So Set Up an Alias</h3>
<p>If you find <code>pnpm</code> hard to type like I do, you can set up an alias to to save you some effort. If you're on Linux or MacOS, just put the following in your shell config (<code>.bashrc</code>, <code>.zshrc</code>, or <code>config.fish</code>):</p>
<pre><code>alias pn=pnpm
</code></pre><p>If you want to set up your alias in Powershell (Windows) you can <a target="_blank" href="https://pnpm.io/installation#adding-a-permanent-alias-in-powershell-windows">see this doc</a>.</p>
<h3 id="heading-how-to-setup-tab-completion">How to Setup Tab-Completion</h3>
<p>There are two ways you can do this in pnpm. Both have their pros and cons.</p>
<p>First I will share with you my favorite method. It's a shell plugin called <code>pnpm-shell-completion</code> and is available for zsh, fish shell, and Powershell core. It only covers the most commonly used commands. If you are Arch Linux and zsh user, you can install it with any AUR helper. For example, if you use <code>yay</code>, run the following command to install it:</p>
<pre><code class="lang-zsh">yay -S pnpm-shell-completion
</code></pre>
<p>Then add the following line in your <code>.zshrc</code> file to load it:</p>
<pre><code class="lang-zsh"><span class="hljs-built_in">source</span> /usr/share/zsh/plugins/pnpm-shell-completion/pnpm-shell-completion.zsh
</code></pre>
<p>Now it should work. If you use any other supported shell, follow the plugin's <a target="_blank" href="https://github.com/g-plane/pnpm-shell-completion">doc</a> to learn how to install it.</p>
<p>The second method comes built-in with pnpm. To setup this style of auto-completion, run the following command:</p>
<pre><code class="lang-shell">pnpm install-completion
</code></pre>
<p>And then follow the steps it gives you. This method covers more commands than the first approach. But it has some limitations – for example it can't auto-complete your <code>package.json</code> scripts. It also, for example, can't auto complete any dependency name that you want to uninstall.</p>
<h2 id="heading-how-to-use-pnpm">How to Use <code>pnpm</code></h2>
<p>Now, you should have pnpm installed with an alias and tab-completion. No more delay – let's see how to use pnpm.</p>
<h3 id="heading-how-to-initialize-a-new-project-using-pnpm">How to Initialize a New Project using <code>pnpm</code></h3>
<p>To get the default <code>package.json</code> in the current directory, run the following command:</p>
<pre><code class="lang-zsh">pnpm init
</code></pre>
<p>Unlike npm, it will not create it interactively and you don't need to specify the <code>-y</code> flag for this.</p>
<h3 id="heading-how-to-install-a-package">How to Install a Package</h3>
<p>To install a package as a dependency, the syntax is:</p>
<pre><code>pnpm add &lt;pkg&gt;
</code></pre><p>To install a package as a dev dependency, you have pass the <code>-D</code> (or <code>--save-dev</code>) flag:</p>
<pre><code>pnpm add -D &lt;pkg&gt;
</code></pre><p>To install a package globally, use the <code>-g</code> flag:</p>
<pre><code>pnpm add -g &lt;pkg&gt;
</code></pre><h3 id="heading-how-to-install-all-dependencies">How to Install All Dependencies</h3>
<p>Suppose you cloned a project from GitHub. It does have a <code>package.json</code> file but no <code>node_modules</code> (you should not track <code>node_modules</code> with Git). Now to install all the dependencies in that <code>package.json</code>, the command is very similar to <code>npm</code>:</p>
<pre><code>pnpm install
</code></pre><p>or</p>
<pre><code>pnpm i
</code></pre><h3 id="heading-how-to-run-a-packagejson-script">How to Run a <code>package.json</code> Script</h3>
<p>This process is also very similar to <code>npm</code>. The explicit way to do it is to use the <code>run</code> command. If you have a script named <code>build</code>, you can execute it with this command:</p>
<pre><code>pnpm run build
</code></pre><p>You can also use <code>pnpm build</code> to do the same thing. This is a shorthand format that can do other things as well. We'll learn more about shorthand very soon in this article.</p>
<h3 id="heading-how-to-run-commands-that-come-with-dependencies">How to Run Commands that Come with Dependencies</h3>
<p>You can run commands that come with dependencies using <code>pnpm exec</code>.</p>
<p>When you install a package, if it has commands specified by the <code>bin</code> field in its <code>package.json</code>, you will get an executable of the same name in your <code>node_modules/.bin</code> directory. Its purpose to execute the corresponding file.</p>
<p><code>pnpm exec</code> prepends <code>./node_modules/.bin</code> to the <code>PATH</code> (that is, <code>PATH=./node_modules/.bin:$PATH</code>) and then executes the given command.</p>
<p>The following is an example that shows installing <code>typescript</code> as a dev dependency and then running the <code>tsc</code> command to create a <code>tsconfig.json</code> file:</p>
<pre><code>pnpm add -D typescript
pnpm exec tsc --init
</code></pre><p>Similar to the <code>pnpm run</code> command, you can also omit <code>exec</code> and just use <code>pnpm tsc</code> to do the same thing. This works when you don't have a conflicting <code>tsc</code> script in your <code>package.json</code>. In the next section we will take a close look at this shorthand syntax.</p>
<p>Note that since <code>pnpm exec</code> has access to all commands resolved by the paths specified in <code>PATH</code>, you may have access to many system commands for example <code>rm</code>, <code>ls</code>, and so on.</p>
<h3 id="heading-how-pnpm-works">How <code>pnpm &lt;command&gt;</code> Works</h3>
<p><code>pnpm &lt;command&gt;</code> works like this:</p>
<ul>
<li>If <code>&lt;command&gt;</code> is a pnpm command (that is <code>add</code>, <code>install</code> and so on), execute that command.</li>
<li>Else if <code>&lt;command&gt;</code> is a script found in <code>package.json</code>, execute <code>pnpm run &lt;command&gt;</code>.</li>
<li>Else execute <code>pnpm exec &lt;command&gt;</code>.</li>
</ul>
<p>So <code>pnpm &lt;command&gt;</code> serves as a convenient shortcut where <code>pnpm exec</code> and <code>pnpm run</code> are explicit commands without fallback.</p>
<h3 id="heading-how-to-update-packages">How to Update Packages</h3>
<p>To update packages to their latest versions based on the specified range in <code>package.json</code>, run this command:</p>
<pre><code>pnpm up
</code></pre><p>To update all dependencies to their latest versions, ignoring ranges specified in <code>package.json</code>, run this:</p>
<pre><code>pnpm up --latest
</code></pre><h3 id="heading-how-to-remove-a-package">How to Remove a Package</h3>
<p>To remove a package from both <code>node_modules</code> and your <code>package.json</code>, you can use <code>pnpm rm</code>. For example if you installed <code>express</code>, you can remove it using:</p>
<pre><code>pnpm rm express
</code></pre><p>To remove a globally installed package, use the <code>-g</code> flag. Below is an example of removing the globally installed package <code>nodemon</code>:</p>
<pre><code>pnpm rm -g nodemon
</code></pre><h2 id="heading-is-there-an-npx-alternative">Is There an <code>npx</code> Alternative?</h2>
<p>Yes – it's the <code>pnpm dlx</code> command. It's very similar to npx. It downloads the specified package from the registry without installing it as a dependency and then runs whatever default command binary it exposes.</p>
<p>For example, you can run the command that <code>cowsay</code> package exposes like below to print ASCII art of a cow saying a string that you pass:</p>
<pre><code>pnpm dlx cowsay hi freeCodeCamp
</code></pre><p>Now you might be wondering, if a package exposes multiple command binaries, what command <code>pnpm dlx</code> chooses as the default? Or how can you explicitly specify a command binary?</p>
<p>Let's see how the default command binary is determined first:</p>
<ul>
<li>If the <code>bin</code> field of <code>package.json</code> has only one entry, then that is used.</li>
<li>Else if there is a command name in the <code>bin</code> field of <code>package.json</code> that matches the package name, ignoring the scope part if any, then that command is used.</li>
<li>Else pnpm can't determine the default command and throws an error with a helpful error message that most likely will answer the second question.</li>
</ul>
<p>To explicitly specify a particular command, you will need to install the package first using the <code>--package</code> option and specify that command after <code>dlx</code>. </p>
<p>For example the package <code>typescript</code> exposes to command binaries <code>tsc</code> and <code>tsserver</code>. Now if you want to run <code>tsc --init</code> to create a <code>tsconfig.json</code> file without having <code>typescript</code> in your <code>node_modules</code> or <code>package.json</code>, you can use <code>pnpm dlx</code> like below:</p>
<pre><code>pnpm --package=typescript dlx tsc --init
</code></pre><h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you've learned what pnpm is and how to install it. We've also covered several common pnpm commands that you will most likely need on a daily basis. </p>
<p>I hope this article helped you get up and running with pnpm. Check out the <a target="_blank" href="https://pnpm.io/motivation">documentation of pnpm</a> to learn more about it.</p>
<p>If you want you can follow me on <a target="_blank" href="https://www.linkedin.com/in/ashutosh-biswas/">LinkedIn</a> and <a target="_blank" href="https://twitter.com/ashutoshbw">Twitter</a> where I share useful coding related things.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
