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
pnpmthroughcorepack:bashcorepack enable pnpm1If the command executes successfully, you typically won’t see any output. This means
corepackhas linked thepnpmcommand into your system.Verify that
pnpmis enabled and check its version:bashpnpm -v1This command should display the current version of
pnpm, for example:bash9.1.01If you see a version number,
pnpmis 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.
bash
$ pnpm -v
! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-9.0.6.tgz
? Do you want to continue? [Y/n]1
2
3
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:
bash
cd /Applications/ServBay/www/my-servbay-app
pnpm install1
2
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:
bash
pnpm add lodash1
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:
bash
pnpm remove lodash1
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:
bash
pnpm run dev
# Or, using the shorthand:
pnpm dev1
2
3
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:
bash
pnpm exec webpack --version
# Or using the shorthand:
pnpx webpack --version1
2
3
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.
