# Using pnpm and pnpm workspaces

**URL:** <https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759>\
**Category:** Support\
**Tags:** settings-config\
**Created:** [August 15, 2019, 11:42am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759 "2019-08-15T11:42:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanmaric](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stefanmaric/32/1024_2.png) [@stefanmaric](https://answers.netlify.com/u/stefanmaric)\
**Post date:** [August 15, 2019, 11:42am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/1 "2019-08-15T11:42:34Z")

</div>

Just as an informative note for future users, here some info on how to workaround Netlify’s CI in order to use `pnpm` instead of `npm` or `yarn` and pnpm workspaces.

1. Remove your `package-lock.json` or `yarn.lock` file and add it to your `.gitignore`, if applicable. Be sure to track `pnpm-lock.yaml` instead.

2. If you’re using workspaces: add `pnpm` itself as a dev dependency on your top-level project, this so it is possible to use `pnpm recursive` commands inside your build scripts

3. Add the `NPM_FLAGS="--prefix=/"` env var to your `netlify.toml` or set it on Netlify’s web dashboard

4. Add `pnpm install` as a build step in your build process, only for Netlify, most probably before anything else. Assuming your build command is `npm run build` , you might add the following script into your top-level `package.json`:

We have been using this setup in prod without issues for a while now. And unlike with yarn, we haven’t experienced issues with missing `node_modules` inside workspaces, since `pnpm` stores every dep inside the top-level `node_modules` (which is the only Netlify saves for cache) and `pnpm install` simply creates symlinks.

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [August 15, 2019, 3:50pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/2 "2019-08-15T15:50:51Z")

</div>

thank you for sharing this!! this is very useful for people!

---

<div class="post-metadata">

**Author:** ![TomPichaud](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/tompichaud/32/10451_2.png) [@TomPichaud](https://answers.netlify.com/u/TomPichaud)\
**Post date:** [May 10, 2020, 9:23am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/3 "2020-05-10T09:23:12Z")

</div>

Hello,

Thank you for sharing this but it seems to be a bit outdated now.

It seems like Netlify should handle pnpm automatically now 🙂

> <https://github.com/netlify/cli/pull/550/commits/131bd5ed4e7121ffdd42f5e2aa6948ea0837c4b3>

But I can’t manage to get it to work, do you?

---

<div class="post-metadata">

**Author:** ![stefanmaric](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stefanmaric/32/1024_2.png) [@stefanmaric](https://answers.netlify.com/u/stefanmaric)\
**Post date:** [May 10, 2020, 10:53am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/4 "2020-05-10T10:53:09Z")

</div>

Hey @TomPichaud

Looking at the linked PR, it seems the author pulled out the pnpm bits from the PR but didn’t change the title/commit message.

Also, proper support for pnpm should be added in the [build image repo](https://github.com/netlify/build-image) instead of the cli one. I’ve looked up and it seems there’s still no support for pnpm.

To be fair, adding direct support for pnpm workspaces caching, specifically, in the way npm and yarn are supported, is a bit complicated. I tried such route myself in the past, before settling on this solution.

I haven’t looked into it, but I guess it is now possible to add pnpm support via the new, beta API for [build plugins](https://github.com/netlify/build).

I’ve actually migrated my projects using pnpm workspaces out of netlify about a week ago, but the workaround I initially posted was still in use, and it worked perfectly: ~200 builds a month for almost a year without issues. So I would recommend you going with it.

A clean `pnpm install` (no cache at all) on netlify’s build servers for a project with 9 workspaces and ~250 direct dependencies takes about 50 seconds and re-linking those dependencies from cache on subsequent builds takes just 1 second.

I have to say pnpm is pretty neat; I haven’t found a better alternative for dependency management on monorepos. 😄

Hope that helps you.

---

<div class="post-metadata">

**Author:** ![stefanmaric](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stefanmaric/32/1024_2.png) [@stefanmaric](https://answers.netlify.com/u/stefanmaric)\
**Post date:** [May 10, 2020, 10:57am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/5 "2020-05-10T10:57:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcayzac](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jcayzac/32/13326_2.png) [@jcayzac](https://answers.netlify.com/u/jcayzac)\
**Post date:** [July 12, 2020, 4:51am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/6 "2020-07-12T04:51:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcayzac](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jcayzac/32/13326_2.png) [@jcayzac](https://answers.netlify.com/u/jcayzac)\
**Post date:** [July 14, 2020, 11:35am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/8 "2020-07-14T11:35:29Z")

</div>

FWIW, I sent a [PR](https://github.com/netlify/build-image/pull/449).

---

<div class="post-metadata">

**Author:** ![sklar](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/sklar/32/14737_2.png) [@sklar](https://answers.netlify.com/u/sklar)\
**Post date:** [August 18, 2020, 7:29am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/9 "2020-08-18T07:29:32Z")

</div>

It seems like disabling the `npm install` by setting the `NPM_FLAGS="--prefix=/"` doesn’t work anymore 🤔  
[The operation was rejected by your operating system](https://answers.netlify.com/t/the-operation-was-rejected-by-your-operating-system/20676)

---

<div class="post-metadata">

**Author:** ![stefanmaric](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stefanmaric/32/1024_2.png) [@stefanmaric](https://answers.netlify.com/u/stefanmaric)\
**Post date:** [August 18, 2020, 11:53am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/10 "2020-08-18T11:53:49Z")

</div>

> [@sklar](#):
>
> It seems like disabling the `npm install` by setting the `NPM_FLAGS="--prefix=/"` doesn’t work anymore 🤔

@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:

```auto
[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](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:

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

```

With:

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

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [August 18, 2020, 4:54pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/11 "2020-08-18T16:54:56Z")

</div>

thank you for sharing this!

---

<div class="post-metadata">

**Author:** ![mudlabs](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mudlabs/32/17164_2.png) [@mudlabs](https://answers.netlify.com/u/mudlabs)\
**Post date:** [November 10, 2020, 12:19am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/12 "2020-11-10T00:19:52Z")

</div>

**It’s not working**

Netlify log errors from:

```auto
$ 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:

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

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

```

My `package.json` scripts are:

```json
"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"
  }

```

---

<div class="post-metadata">

**Author:** ![mudlabs](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mudlabs/32/17164_2.png) [@mudlabs](https://answers.netlify.com/u/mudlabs)\
**Post date:** [November 10, 2020, 4:11am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/13 "2020-11-10T04:11:32Z")

</div>

I had to update my build `command` in `netlify.toml`

```auto
[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_.

```auto
$ 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

```

---

<div class="post-metadata">

**Author:** ![stefanmaric](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/stefanmaric/32/1024_2.png) [@stefanmaric](https://answers.netlify.com/u/stefanmaric)\
**Post date:** [November 11, 2020, 3:14pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/14 "2020-11-11T15:14:24Z")

</div>

@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](https://github.com/stefanmaric/test-pnpm-on-netlify/blob/main/package.json)

---

<div class="post-metadata">

**Author:** ![avi12](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/avi12/32/14950_2.png) [@avi12](https://answers.netlify.com/u/avi12)\
**Post date:** [July 3, 2021, 8:21pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/15 "2021-07-03T20:21:48Z")

</div>

I don’t use PNPM workspaces, but I do use Netlify Functions  
It seems like, despite @stefanmaric’s [example repository setup](https://github.com/stefanmaric/test-pnpm-on-netlify), my Netlify build fails:

```auto
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](https://github.com/modfy/netlify-plugin-pnpm-preinstaller)

---

<div class="post-metadata">

**Author:** ![bpierre](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/bpierre/32/28811_2.png) [@bpierre](https://answers.netlify.com/u/bpierre)\
**Post date:** [August 27, 2021, 2:04pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/16 "2021-08-27T14:04:02Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Anoesj](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/anoesj/32/43918_2.png) [@Anoesj](https://answers.netlify.com/u/Anoesj)\
**Post date:** [October 9, 2021, 11:54am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/17 "2021-10-09T11:54:40Z")

</div>

Using `--prefix=/dev/null` didn’t seem to work anymore when using NPM 7. I can confirm that using `--version` instead works 😃. 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`.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [October 9, 2021, 12:30pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/18 "2021-10-09T12:30:46Z")

</div>

Hi @Anoesj,

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

---

<div class="post-metadata">

**Author:** ![aparajita](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/aparajita/32/32277_2.png) [@aparajita](https://answers.netlify.com/u/aparajita)\
**Post date:** [December 14, 2021, 1:03am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/19 "2021-12-14T01:03:53Z")

</div>

Please do! pnpm is no longer a niche product.

---

<div class="post-metadata">

**Author:** ![hillary](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hillary/32/21132_2.png) [@hillary](https://answers.netlify.com/u/hillary)\
**Post date:** [December 22, 2021, 11:10pm UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/21 "2021-12-22T23:10:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![accuser](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/accuser/32/30857_2.png) [@accuser](https://answers.netlify.com/u/accuser)\
**Post date:** [March 23, 2022, 10:22am UTC](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759/23 "2022-03-23T10:22:56Z")

</div>

Can we please have `pnpm` properly supported? [Projects using pnpm can now be deployed with zero configuration – Vercel](https://vercel.com/changelog/projects-using-pnpm-can-now-be-deployed-with-zero-configuration)

[Next page](https://answers.netlify.com/t/using-pnpm-and-pnpm-workspaces/2759.md?page=2)
