How can I automatically install npm peer dependencies?

I’m currently dealing with a project where I’m trying to install Angular2, but upon running the installation command, I encounter several warnings about unmet peer dependencies.

npm install --save angular2
[email protected] /Users/user/Projects/myproject
├── [email protected]
├── UNMET PEER DEPENDENCY es6-promise@^3.0.2
├── UNMET PEER DEPENDENCY es6-shim@^0.33.3
├── UNMET PEER DEPENDENCY [email protected]
├── UNMET PEER DEPENDENCY [email protected]
└── UNMET PEER DEPENDENCY [email protected]

npm WARN [email protected] requires a peer of es6-promise@^3.0.2 but none was installed.
npm WARN [email protected] requires a peer of es6-shim@^0.33.3 but none was installed.
npm WARN [email protected] requires a peer of [email protected] but none was installed.
npm WARN [email protected] requires a peer of [email protected] but none was installed.
npm WARN [email protected] requires a peer of [email protected] but none was installed.

I want to know if there’s an option or flag that npm provides which could automatically install these peer dependencies along with the main package. Manually typing in each peer dependency and ensuring I have the right versions is tedious and I’d prefer a more efficient process.

For instance, I often find myself running this command:

npm install --save [email protected] es6-promise@^3.0.2 es6-shim@^0.33.3 [email protected] [email protected] [email protected]

Is there a simpler way to handle this?

I hit this exact issue with older Angular versions. There’s no native npm flag for it, but yarn handles peer dependencies way better. Run yarn add angular2 and it’ll either prompt you to install missing peer deps or do it automatically based on your config. I also wrote a script that parses npm warnings, grabs the peer dependency names and versions, then runs one install command for all of them. Saves tons of time on projects with heavy peer dependency requirements.

I’ve dealt with this for years across different projects. npm’s intentionally cautious about peer dependencies since they can break things with version conflicts. What works for me: use npm-check-updates with a simple bash script that pulls peer dependency info from npm output. Just pipe the npm install output to grep for “UNMET PEER DEPENDENCY” lines, then format them into one install command. Or switch to pnpm - it handles peer dependencies way better than npm. You’ll get clearer warnings and it’ll offer to install them for you. The manual approach you’re using isn’t wrong though. It’s just npm being conservative so it doesn’t break your dependency tree with automatic installs.

npm doesn’t have a built-in flag for auto-installing peer deps. Try the npm-install-peers package - just run npx install-peerdeps <package-name> and it handles everything automatically. Way easier than manually typing each dependency!