npm has it's advantages, albeit only a few other than being already installed in most cases. But
pnpm has far more!
In this exercise we'll cover only the install phase and assume we are building in docker and using
npm i -g pnpm or use the inline script at Installation | pnpm.
I'm using the following
package.json. Notice the usage of both public and private packages.
There is a peerDependency of
bcrypt which will require compilation during the install. This should further increase our install time.
FROM node:alpine AS builder # # Needed for bcrypt node module. # RUN apk add --virtual builds-deps build-base python3 # # Install the pnpm. # RUN curl -f https://get.pnpm.io/v6.js | node - add --global pnpm WORKDIR /app COPY .npmrc /root/.npmrc COPY package.json . RUN time pnpm install --network-concurrency=32 --prod RUN rm -rf ~/.npmrc COPY src src RUN npm run build RUN rm -rf src ENTRYPOINT ["node", "/app/dist/main.js"]
We'll compare using
npm install vs.
pnpm install --network-concurrency=32 --prod.
Notice how we've increased our concurrency using
--network-concurrency. The default is 16. Increasing this number to 32 shaved off 6-10 seconds on average.
Running a straight
npm install resulted in build times averaging 3+ minutes whereas running a
pnpm install --network-concurrency=32 --prod completed under an average of 45 seconds.
--prodwill prevent your
devDependenciesfrom being installed. Use it! See pnpm install | pnpm for the full list of options.
This time saving command can increase your productivity and reduce your build time, CI/CD costs, and quality of life.