Angular 7 CLI项目node_modules达660Mb,求优化方案及同行现状问询
Hey there, you’re definitely not alone in noticing this—lots of Angular developers saw a jump in node_modules size when moving from v6 to v7. Let’s walk through why this happens, how to slim it down, and what others are seeing.
Why the Size Jump from Angular 6 to 7?
The main culprits behind the doubled size are:
- Toolchain Overhaul: Angular 7 introduced a more robust build system with
@angular-devkit/build-angular(which replaces older CLI build tools). This package brings in additional dependencies for better build optimization, but adds to the overall size. - Updated Dependencies: RxJS 6.x (required in Angular 7) has a different dependency structure than the version used in v6, and TypeScript 3.x (also a requirement) includes more files and sub-dependencies compared to TypeScript 2.x in v6.
- Expanded Dev Dependencies: Default Angular 7 projects include more comprehensive testing and linting tools (like updated Karma, Jasmine, and Codelyzer versions) that add to the dev dependency footprint.
How to Optimize Your node_modules Size
Here are practical steps to reduce disk usage:
- Switch to pnpm: This package manager uses hard links and shared dependency stores, so it can cut your
node_modulessize by 60-70% (down to ~200MB for a default Angular 7 project). Install it withnpm install -g pnpm, then runpnpm installinstead ofnpm install. - Use
npm ciInstead ofnpm install:npm ciinstalls dependencies strictly from yourpackage-lock.json, avoiding unnecessary duplicate packages that can creep in with regularnpm install. It’s also faster! - Clean Up Unused Dev Dependencies: If you don’t need certain tools:
- Remove e2e testing tools: Uninstall
protractorand related packages, then delete thee2efolder. - Ditch linting: Remove
codelyzerandtslintif you use a different linter or don’t need one.
- Remove e2e testing tools: Uninstall
- Clear npm Cache: Run
npm cache clean --forceto delete cached packages, then reinstall withnpm install—this can eliminate stale or duplicated cached files. - Run
npm prune: This command removes any packages that aren’t listed in yourpackage.json(though less likely to help with a fresh project, it’s worth a shot).
What Other Developers Are Seeing
From community discussions, a default Angular 7 project’s node_modules typically ranges from 600-700MB—so your 660MB is totally normal. Angular 6 projects were usually around 300-350MB, so the jump is noticeable but expected due to the tooling upgrades.
One thing to keep in mind: the size of node_modules doesn’t affect your production build output. Angular’s tree-shaking and optimization will strip out unused code, leaving you with a lean dist folder regardless of how big node_modules is.
内容的提问来源于stack exchange,提问作者plvice

