42

How (or on which file) to set true to do auto install dependencies ?

my terminal error:

my terminal error

hint: If you want peer dependencies to be automatically installed, set the "auto-install-peers" setting to "true". hint: If you don't want pnpm to fail on peer dependency issues, set the "strict-peer-dependencies" setting to "false".

auto-install-peers = true
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
fedoraname1
  • 531
  • 1
  • 3
  • 8
  • create a file ".npmrc" in root directory of your project and set this flag.--------------- . pnpm config set auto-install-peers true ----------------------------------------------------. pnpm installs peer deps only after 7.1.4 version, Update pnpm in your machine. https://pnpm.io/installation – Santosh Karanam Jul 12 '23 at 14:36

5 Answers5

71

pnpm uses npm's configuration formats. Hence, you should set configuration the same way you would for npm:

pnpm config set auto-install-peers true

Note: The above command uses the default config location which stores the setting for the local user account (at ~/.npmrc for linux, or at %USERPROFILE%\.npmrc for Windows). To store the setting inside your project in a .npmrc file that can be checked in to version control, you can use the method pointed out by @ZoltanKochan, or equivalently append --location project to the command:

pnpm config set auto-install-peers true --location project
mrmashal
  • 1,721
  • 18
  • 21
  • how to remove this configuration? and also this didn't update the dependency listed in the package.json when peers installed – Bryan Lumbantobing Jul 28 '22 at 06:50
  • 1
    @BryanLumbantobing `pnpm config delete auto-install-peers` would remove the setting (or you can manually edit the corresponding `.npmrc` file. I edited the answer to clarify this). But you shouldn't expect `package.json` to be updated when setting a config value or installing the dependencies. We're just telling pnpm to install the [peer dependencies](https://nodejs.org/en/blog/npm/peer-dependencies/). – mrmashal Jul 28 '22 at 22:59
  • is that a best practice? I see that `npm` also does that. it automatically installs peersDeps without the need to list the peerDeps in our package.json. but `yarn` doesn't – Bryan Lumbantobing Aug 01 '22 at 08:45
  • I'm not sure if I could quite grasp your point, and I'm not into developing js plugins, but generally, AFAIK, best practice is to avoid depending on modules that you don't explicitly declare in your `package.json`, since it might break sometihng on dependency updates. npm has decided to flatten the deps into `node_modules` root, and to somehow mix up everything in there (including peerDeps) and allow you to use them undeclared. If you want to do the same with pnpm, you need to "[shamefully-hoist](https://pnpm.io/npmrc#shamefully-hoist)" them :D – mrmashal Aug 05 '22 at 19:20
  • pnpm, by default, only installs (links) peerDeps if they have already been installed by some other dep. – mrmashal Aug 05 '22 at 19:21
32

You need to create a .npmrc at the root of your project with the following content:

auto-install-peers=true

The answer from mrmashal will work also but only for you locally. So, when someone else fetches your repository, they will not have the peers automatically installed.

Zoltan Kochan
  • 5,180
  • 29
  • 38
12

npm from v7 does auto-install, pnpm doesn't

npm starting from v7. Does install Peer Dependencies automatically https://github.com/npm/rfcs/blob/main/implemented/0025-install-peer-deps.md.

pnpm doesn't do it automatically. Even at this stage. https://github.com/pnpm/pnpm/discussions/3995#discussioncomment-1893230

npm does the same way only with .npmrc

auto-install-peers = true

auto-install-peers=true now makes pnpm work the same way as npm v7. From pnpm v7.1.3 (ref)

And the with .npmrc and not automatic was a choice by the developers involved. There are those who were for and who weren't. (ref1, ref2)

.npmrc

To do it you have to create a .npmrc file and add:

auto-install-peers = true

This is the best way. Because it creates consistency for all developers consuming the project and repo. Same config.

So in simplified terms, if you have some packages that require peers just add the config. You have a nice warning that reminds you in case there are packages with peer-dependencies.

https://github.com/pnpm/pnpm/discussions/3995#discussioncomment-2797582

auto-install-peers=true now makes pnpm work the same way as npm v7. From pnpm v7.1.3

Does an --auto-install-peers arg exists ? (No)

There is no --auto-install-peers arg.

Can check here the feature ask here https://github.com/pnpm/pnpm/issues/5284

Denied.

And the why is understandable. .npmrc is better for consistency. So that people pnpm install and it always works the same. No forgetting anything.

Using install-peerdeps

I advise using .npmrc. -> Native. straight forward.

Note: you may consider this tool. If you fall into some of pnpm bugs (ex: 1, ) and inconsistencies with peer-dependencies handling. Many issues are open. I would go with .pnpmrc first. If any issues. I would use this tool.

https://www.npmjs.com/package/install-peerdeps

install-peerdeps supports pnpm.

The tool is mentioned in eslint-config-airbnb for example.

Example:

npx install-peerdeps --pnpm <your-package>

# or

npx install-peerdeps -P <your-package>

# as dev dep
npx install-peerdeps -P -D <your-package>

# Peers only
npx install-peerdeps -P -D --only-peers <your-package>
# or
npx install-peerdeps -P -D -o <your-package>

The package will automatically add the dependencies to package.json in dependencies or devDependencies depending on the used flag.

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
8

Remove the node modules by running:

rm -rf node_modules

Then again run:

pnpm install
Abahmad
  • 97
  • 1
  • 1
    First create .npmrc with `auto-install-peers=true` set. Then `rm -rf node_modules` and finally pnpm install. I believe pnpm caches the config so we need to delete the node_modules so it re-caches again. – Dale Ryan Apr 02 '23 at 11:20
6

I had to create .npmrc at the root of the project with auto-install-peers=true, then delete the pnpm-lock.yaml file and run pnpm i --shamefully-hoist

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122