How to decrease deployment time by 44% with pnpm

Serhii Shramko

Serhii Shramko /

3 min read β€’ --- views

Introduction

pnpm logo on gay background

I found article from Vercel about Projects using pnpm can now be deployed with zero configuration and I wanted to try it.

Why Migrate to pnpm?

Before diving into the migration process, it’s important to understand why pnpm is beneficial:

  1. Speed πŸš€: pnpm installs dependencies faster than npm by using a unique package linking mechanism.
  2. Disk Space πŸ”Ž: It saves disk space by using a global store and hard links.
  3. Consistency πŸ’‘: pnpm ensures that dependencies are installed in a way that is more consistent with the version specified in your package.json.

Installation of pnpm

First, you need to install pnpm globally. You can do this by running:

brew install pnpm

Migration Steps

1. Clean the Project

Remove the node_modules directory and the package-lock.json file. This step is crucial to ensure that there are no leftover artifacts from npm that might interfere with pnpm.

rm -rf node_modules package-lock.json

2. Initialize pnpm

Run the following command to initialize pnpm in your project directory:

pnpm install

This command will create a pnpm-lock.yaml file and install all the dependencies listed in your package.json.

3. Verify Dependency Installation

Check that all dependencies have been installed correctly by running:

pnpm list

This command lists all the installed dependencies and their versions. Make sure that there are no missing or outdated packages.

pnpm list command result in terminal

Updating Scripts

1. Update npm Scripts

In your package.json, update any npm scripts to use pnpm where necessary. For example, if you have a script for installing dependencies, change it from:

"scripts": {
"install": "npm install"
}

to:

"scripts": {
"install": "pnpm install"
}

2. Update CI/CD Pipelines

If your project uses CI/CD pipelines, ensure that the build scripts and installation commands are updated to use pnpm. For example, in a GitHub Actions with pnpm workflow, you might update the step as follows:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 9
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'
      - name: Install
        run: pnpm i
      - name: Run Tests // All your CI commands
        run: pnpm run test

Handling Issues and Troubleshooting

During the migration, you might encounter some issues.

Error: Unable to locate executable file: pnpm. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.

I stuck with this error when I tried to run the pnpm command in GitHub Actions.

The solution was to use the pnpm/action-setup@v4 before actions/setup-node@v4. So simply just copy the above code and paste it in your .yml file.

Conclusion

This migration helped me to reduce the time of deployment by 44% and the size of node_modules by 5%.

Deployment time on vercel dashboard. Around 1 seccond

More info how my blog works you can find there How my site works under the hood

Share it: