How to Enable and Use pnpm in ServBay
pnpm
is a modern and efficient Node.js package manager, designed to address the pain points developers face when handling dependencies with traditional managers such as npm
and yarn
. By leveraging a content-addressable filesystem and using hard and symbolic links, pnpm
dramatically reduces disk space usage and accelerates installation speeds. For developers using ServBay for Node.js development, mastering pnpm
can greatly improve the efficiency of project dependency management.
The Node.js package provided by ServBay comes integrated with corepack
, an official experimental tool from Node.js for managing and bootstrapping specific versions of package managers like npm
, yarn
, and pnpm
. Typically, after installing Node.js via ServBay, you can enable pnpm
using the corepack
command.
Enabling pnpm
If you find that the pnpm
command is not immediately available, it's usually because it hasn't been enabled yet via corepack
. To activate it, follow these steps:
Open your Terminal application.
Enter the following command to enable
pnpm
throughcorepack
:bashcorepack enable pnpm
1If the command executes successfully, you typically won’t see any output. This means
corepack
has linked thepnpm
command into your system.Verify that
pnpm
is enabled and check its version:bashpnpm -v
1This command should display the current version of
pnpm
, for example:bash9.1.0
1If you see a version number,
pnpm
is now ready to use.
corepack
Download Prompts
When running pnpm
commands, you may encounter prompts like the following. This means corepack
is downloading or updating the specified version of pnpm
for you. This is expected behavior, ensuring that you are using either the required or the latest version for your project.
$ pnpm -v
! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-9.0.6.tgz
? Do you want to continue? [Y/n]
2
3
Type Y
and press Enter to allow corepack
to proceed with the download and to continue executing the pnpm
command. Once the download is finished, the command will execute normally and display the version number (or whichever pnpm
command result you’ve entered).
Key Advantages and Usage Examples of pnpm
The core idea behind pnpm
is efficient dependency management. All packages are stored in a single, globally content-addressable store; each package version is saved only once. When a project requires a dependency, pnpm
creates a hard link from the global store, and the node_modules
directory in your project is built using symbolic links to create a flat structure. Unlike npm
's "phantom dependency" problem, pnpm
ensures strict module boundaries: only directly declared dependencies are accessible in your project.
This approach offers the following significant benefits:
- Saves Disk Space: Different projects share the same versioned dependency files, avoiding redundant storage.
- Faster Installation: In most cases, dependencies already exist in global storage, so only links are created—much faster than copying or downloading again.
- Strict Dependency Structure: Prevents phantom dependencies and makes project relationships clearer and more manageable.
Here are some typical pnpm
command examples for use in your ServBay environment:
Suppose your project is located in a subdirectory under ServBay’s website root, such as /Applications/ServBay/www/my-servbay-app
.
Installing Project Dependencies
Navigate to your project directory and run pnpm install
to install all dependencies defined in your package.json
:
cd /Applications/ServBay/www/my-servbay-app
pnpm install
2
pnpm
checks the global store, downloads missing packages, and creates the necessary links in your project’s node_modules
.
Adding a New Dependency
To add a new dependency, such as lodash
, run:
pnpm add lodash
This will download lodash
(if it’s not already in the global store), create the relevant links, and automatically update your project’s package.json
and pnpm-lock.yaml
files.
Removing a Dependency
To remove a dependency that is no longer needed, like lodash
:
pnpm remove lodash
This removes lodash
from your project dependencies and updates package.json
and pnpm-lock.yaml
.
Running Scripts
pnpm
can also be used to run scripts defined in the scripts
section of your package.json
:
pnpm run dev
# Or, using the shorthand:
pnpm dev
2
3
Executing Package Binaries (pnpx)
Similar to npx
, pnpm
offers pnpx
(or the command pnpm exec
) to run binaries installed locally in your project's node_modules/.bin
directory, without requiring global installation:
pnpm exec webpack --version
# Or using the shorthand:
pnpx webpack --version
2
3
Summary
By running corepack enable pnpm
, you can easily activate pnpm
in the Node.js environment provided by ServBay. Taking advantage of pnpm
’s efficient dependency management capabilities allows you to significantly speed up install times and boost your Node.js project productivity, while also saving valuable disk space. For ServBay users seeking an outstanding development experience, pnpm
is a highly recommended tool.