Using pnpm and pnpm workspaces

I cannot edit my initial post, so pushing an update as a comment:

Remove the @3 parts from the commands, so 1) you get the latest pnpm version and 2) it actually saves you a few seconds when building from cache because if you specify the version, npx always fetches such version from the registry even if the package is available locally in node_modules/.

2nd update: you can skip the -r flag (--recursive) as well since, starting with pnpm@4, recursive is the default behavior for workspaces.

2 Likes

One word of caution: Netlify only restores node_modules if there’s a root-level package.json (they do persist the folder to the cache either way), so you may have to restore the folder yourself if using pnpm workspaces and don’t have a root-level package.json, otherwise pnpm will download all modules every build.

1 Like

FWIW, I sent a PR.

It seems like disabling the npm install by setting the NPM_FLAGS="--prefix=/" doesn’t work anymore :thinking:
The operation was rejected by your operating system

@sklar it seems that either npm v6 added some permission checks or the Netlify build image changed the build user’s groups.

I tried with /dev/null and it seems to be working:

[build.environment]
    NPM_FLAGS="--prefix=/dev/null"

I made a test repo for it since I’m not really hosting those projects I was using pnpm with at Netlify anymore. You might see it here: https://github.com/stefanmaric/test-pnpm-on-netlify

Another thing I noticed, is that either pnpm or Netlify changed the default POSIX shell the package.json scripts uses, therefore the test command was failing on me. So update:

"prebuild": "[[ $CI = true ]] && npx pnpm install --store=node_modules/.pnpm-store || echo skiping pnpm install"

With:

"prebuild": "test \"$CI\" = true && npx pnpm install -r --store=node_modules/.pnpm-store || echo skiping pnpm install",

Hope that helps.

2 Likes

thank you for sharing this!

1 Like

It’s not working

Netlify log errors from:

$ pnpm run build
bash: pnpm: command not found
 ​
┌─────────────────────────────┐
│   "build.command" failed    │
└─────────────────────────────┘
​
Error message
Command failed with exit code 127: pnpm run build
​
Error location
In build.command from netlify.toml:
pnpm run build

Resolved config
build:
command: pnpm run build
commandOrigin: config
environment:
- NPM_FLAGS
publish: /opt/build/repo/.dist


My netlify.toml is:

[build]
    publish = ".dist"
    command = "pnpm run build"

[build.environment]
    NPM_FLAGS="--prefix=/dev/null"


My package.json scripts are:

"scripts": {
    "prebuild": "test \"$CI\" = true && npx pnpm install -r --store=node_modules/.pnpm-store || echo skipping pnpm install",
    "build": "gulp build --dir .dist",
    "dev": "gulp dev --dir .dev"
  }

I had to update my build command in netlify.toml

[build]
    publish = ".dist"
    command = "pnpm run build || ( npm install pnpm && pnpm run build )"

[build.environment]
    NPM_FLAGS = "--prefix=/dev/null"


Of course that created a package-lock.json file, which we don’t want or need, because pnpm will create pnpm-lock.yaml file. To fix that, create an .npmrc file in the repo root, and add the line package-lock=false. Then add package-lock.json to .gitignore.


I do get a warning after the prebuild cammand is called. But everything seems to be working so… whatever.

$ pnpm run build || npm install pnpm && pnpm run build
> mudlify@1.0.0 prebuild /opt/build/repo
> test "$CI" = true && npx pnpm install -r --store=node_modules/.pnpm-store || echo skiping pnpm install
|  WARN  A pnpm-lock.yaml file exists. The current configuration prohibits to read or write a lockfile

@mudlabs I believe you don’t really need to use pnpm as the build command, you can keep npm run build and use any CLI tools you need inside the package.json scripts, provided they were installed in your workspace, as seen here in the test repo I provided: https://github.com/stefanmaric/test-pnpm-on-netlify/blob/main/package.json

I don’t use PNPM workspaces, but I do use Netlify Functions
It seems like, despite @stefanmaric’s example repository setup, my Netlify build fails:

A Netlify Function failed to require one of its dependencies.
11:10:57 PM:   Please make sure it is present in the site`s top-level "package.json".
​
11:10:57 PM:   In file "/opt/build/repo/functions/***.js"
11:10:57 PM:   Cannot find module 'axios'

…and then I found this beautiful plugin

The --prefix flags weren’t working for me, so I ended up passing --version to prevent npm to do anything before the build:

[build.environment]
  NPM_FLAGS = "--version"
[build]
  command = "npx pnpm install --store=node_modules/.pnpm-store && npx pnpm build"
1 Like

Using --prefix=/dev/null didn’t seem to work anymore when using NPM 7. I can confirm that using --version instead works :smiley:. I have to say though, this is basically a hack and it would be great if Netlify provided a way to skip running npm install automatically, e.g. with a NPM_SKIP_INSTALL = true option in netlify.toml.

1 Like

Hi @Anoesj,

Thanks for the suggestion. we’d pass it on to the team.

2 Likes

Please do! pnpm is no longer a niche product.

1 Like

Thanks for your feedback, @aparajita. I have sent this over to the Product team!

4 Likes

Can we please have pnpm properly supported? Projects using pnpm can now be deployed with zero configuration – Vercel

4 Likes

Hey there, @accuser :wave:

Thanks so much for reaching out! I have shared your request directly with our product team. We will follow up here with more news when we have it!

2 Likes

chiming in to +1. It is now 3 years since the issue was raised, pnpm is de facto standard for workspaces, and vercel has it now.

i’m supposed to know netlify well, just wasted an hour following old blogposts telling me to write to --prefix=/dev/null and then getting EEXIST: file already exists, mkdir '/dev/null'.

please add native support for pnpm.

3 Likes

Hey there, @sw-yx :wave:

Thanks so much for chiming in. I have passed your feedback directly to our product team. We will follow up here with future updates when we have them!

Hey there, folks! Thanks for all your patience on this thread. I wanted to announce that Netlify now has pnpm support out of the box :rocket:

You can read more about it on our blog, and you can visit the docs to learn how to get started.

Happy building :netliconfetti:

2 Likes