Is there a way to automatically install npm peer dependencies?

I’m using React and whenever I try to install it, I keep encountering these annoying peer dependency warnings.

npm install --save react
[email protected] /Users/sarah/workspace/my-project
├── [email protected]
├── UNMET PEER DEPENDENCY prop-types@^15.6.0
├── UNMET PEER DEPENDENCY react-dom@^18.0.0
├── UNMET PEER DEPENDENCY lodash@^4.17.21
├── UNMET PEER DEPENDENCY axios@^1.2.0
└── UNMET PEER DEPENDENCY moment@^2.29.0

npm WARN [email protected] requires a peer of prop-types@^15.6.0 but none was installed.
npm WARN [email protected] requires a peer of react-dom@^18.0.0 but none was installed.
npm WARN [email protected] requires a peer of lodash@^4.17.21 but none was installed.
npm WARN [email protected] requires a peer of axios@^1.2.0 but none was installed.
npm WARN [email protected] requires a peer of moment@^2.29.0 but none was installed.

Is there a command line option in npm that can take care of peer dependencies for me? I’d prefer not to install each one manually like this:

npm install --save [email protected] prop-types@^15.6.0 react-dom@^18.0.0 lodash@^4.17.21 axios@^1.2.0 moment@^2.29.0

There must be a simpler method, right?

Try npm install --install-peers if ur on npm 7+, but it’s pretty buggy. I just install them manually - more work but you kno exactly what versions u’re getting. That peer dependency list looks weird for React tho. Vanilla React shouldn’t need all those as peers.

Nope, npm doesn’t have a built-in flag for auto-installing peer dependencies. I hit this same wall when I started working with React projects a few years ago. I switched to yarn since it had yarn install --peer (though that’s deprecated now). These days I just use npm install --legacy-peer-deps - it won’t auto-install peers but kills those annoying warnings. For a real fix, grab the install-peerdeps package: npm install -g install-peerdeps, then run install-peerdeps react to get both the package and its peers automatically. Saves me tons of time.

npm doesn’t auto-install peer dependencies, but I’ve found a solid workaround. Run npx install-peerdeps react and it’ll grab React plus all its peer deps in one shot - no global install needed. Found this after dealing with those same annoying warnings. You can also use npm install --legacy-peer-deps to kill the warning spam, but that won’t actually install the missing dependencies. Side note: lodash and moment aren’t React peer deps - that output seems weird for a standard React install.