fedoraname1
fedoraname1

Reputation: 671

pnpm peer dependencies auto-install

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

Upvotes: 56

Views: 96379

Answers (6)

Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18538

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.

Upvotes: 9

USS Franck
USS Franck

Reputation: 9

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

Upvotes: 0

Mohamed Allal
Mohamed Allal

Reputation: 20870

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.

Upvotes: 20

mrmashal
mrmashal

Reputation: 1863

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

Upvotes: 83

Zoltan Kochan
Zoltan Kochan

Reputation: 7636

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.

Upvotes: 38

Abahmad
Abahmad

Reputation: 117

Remove the node modules by running:

rm -rf node_modules

Then again run:

pnpm install

Upvotes: 10

Related Questions