npm: You must install peer dependencies yourself [Solved]

avatar
Borislav Hadzhiev

Last updated: May 25, 2023
4 min

banner

# npm: You must install peer dependencies yourself [Solved]

The npm warning "A requires a peer of B but none is installed. You must install peer dependencies yourself" occurs because npm no longer installs peer dependencies automatically after version 3.

To solve the issue install the second package from the error message with the npm install --save-dev command.

Here is the complete warning message.

For example, suppose that you got the following warning message:

In this case, you'd have to run the following command.

shell
npm install --save-dev "typescript@>=2.7"

install peer dependency as dev dependency

The package you should install as a dev dependency is specified in your warning message.

The format of the warning message is:

  • npm WARN {PACKAGE_A} requires a peer of {PACKAGE_B} but none is installed. You must install peer dependencies yourself.

In this case, you have to run the following command.

shell
npm install --save-dev "{PACKAGE_B}"

Notice that we install the second package from the error message.

Make sure to include the version in the installation command.

For example, suppose that you get the following warning message:

  • npm WARN @apideck/better-ajv-errors@0.3.2 requires a peer of ajv@>=8 but none is installed. You must install peer dependencies yourself.

In this case, you have to run the following command.

shell
npm install --save-dev "ajv@>=8"

npm install peer dependency package

The --save-dev option installs the package in your development dependencies.

In other words, it adds the package to the devDependencies object in your package.json file.

If the issue persists, try to install the latest version of the second package from the warning message.

For example, suppose that you get the following warning message:

  • npm WARN @apideck/better-ajv-errors@0.3.2 requires a peer of ajv@>=8 but none is installed. You must install peer dependencies yourself.

Try to run the following command if the warning persists.

shell
npm install --save-dev "ajv@latest"

# Try to update npm to the latest version

If the issue persists, try to update npm to the latest version.

Open your terminal and run the following command.

shell
npm install -g npm@latest

update npm to latest version

If you get a permissions error on macOS or Linux, prefix the command with sudo.

shell
sudo npm install -g npm@latest

If you get a permissions error on Windows, open CMD (Command Prompt) as an administrator and rerun the command.

  1. Click on the Search field and type CMD.
  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Rerun the command.
cmd
npm install -g npm@latest

# Delete your node_modules directory and reinstall your modules

If the issue persists, try to delete your node_modules directory and reinstall your modules.

If you are on macOS or Linux, open your terminal in your project's root directory (where your package.json) file is and run the following commands.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, open CMD (Command Prompt) in your project's root directory and run the following commands.

shell
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

# Run the installation command with the --legacy-peer-deps option

If the issue persists, try to run the installation command with the --legacy-peer-deps option.

shell
npm install --legacy-peer-deps

Or to install a specific package:

shell
npm install axios --legacy-peer-deps

Make sure to replace axios with the name of your specific package.

The --legacy-peer-deps flag ignores all peer dependencies when installing (in the style of npm version 4 through 6).

# Ignore the warnings if the issue persists

If the issue persists, you can ignore the warning messages.

Peer dependencies are listed in the package.json file of some of the packages you've installed.

Packages specify a peerDependencies object in their package.json file to set which versions of other packages are compatible with this version of the installed package.

If the specified versions of these packages are already present in your node_modules directory, no warnings are printed.

If the specified versions are not present in node_modules, then they won't be installed by NPM and a warning is printed.

The warning is also printed when you have other versions of the same packages installed.

You can just ignore these warnings as they most likely don't relate to you and you either have other versions of the specified (peerDependencies) packages installed or you don't need them.

The peerDependencies object is used to inform you which versions of other packages are compatible with this version of the package in case you decide to install these packages at a later point in time.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev