58

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
2
  • 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. pnpm.io/installation Commented Jul 12, 2023 at 14:36
  • @SantoshKaranam its not mentioned anymore I think in pnpm v11. Is that correct? its not relevant anymore now? Commented May 29 at 20:11

6 Answers 6

88

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
Sign up to request clarification or add additional context in comments.

how to remove this configuration? and also this didn't update the dependency listed in the package.json when peers installed
@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.
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
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" them :D
pnpm, by default, only installs (links) peerDeps if they have already been installed by some other dep.
38

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.

Comments

21

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.

is this still an issue with pnpm.. Since I already get warings of npm saying that auto-install-peers will stop working in the next major version of npm. Which might be confusing, I really hope pnpm got it resolved today
10

Remove the node modules by running:

rm -rf node_modules

Then again run:

pnpm install

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.
10

Starting from version 7, pnpm supports auto-installing peer dependencies by default.

To enable/disable auto-installing peer dependencies, you can set/unset auto-install-peers using the following command.

pnpm config set auto-install-peers [true/false]

The above command will set this option globally in global .npmrc file.

To do it locally, add --location=project.

Also, you can manually create/edit .npmrc file.

After setting auto-install-peers option, you might need to delete node_modules directory, and pnpm-lock.yaml file and then run pnpm i --shamefully-hoist.

Original answer

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.

This is what solved the issue for me.
And what about pnpm today?
0

Modify or create a .npmrc file at the root of your project and insert the following instruction, which instructs pnpm to auto-install peer dependencies:

auto-install-peers=true

Then do a pnpm install or pnpm install --force

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.