如何使用Angular CLI移除通过ng add安装的包?
ng add Using Angular CLI Great question! Let’s break this down clearly for you:
First, it’s important to note that as of now, Angular CLI doesn’t have an official, fully functional ng rm command that can seamlessly reverse the changes made by ng add—that’s exactly why your attempt with ng rm didn’t work. Remember, ng add does more than just download dependencies like npm install <package>: it uses schematics to modify your project config, add polyfills, generate package-specific initialization code, and more. Removing these packages requires reversing all those targeted changes, which can vary depending on the package you installed.
Here’s how you can properly handle the removal:
Check for a package-specific removal schematic
Some packages that supportng addalso provide a dedicated schematic to clean up their changes. You can check the package’s documentation for this, or try running a command like:ng generate <package-name>:removeIf this schematic exists, it will automatically reverse config edits, delete generated code, and clean up related dependencies for you.
Manual removal (when no removal schematic exists)
If there’s no built-in cleanup tool, you’ll need to reverse theng addchanges step by step:- Uninstall the core package via your package manager:
npm uninstall <package-name> # Or use yarn if that's your setup yarn remove <package-name> - Clean up project configuration files:
Go through files likeangular.json,package.json,tsconfig.json, andpolyfills.tsto remove any entries the package added (such as build configurations, scripts, polyfill imports, etc.). - Delete generated code:
Remove any components, modules, or utility files thatng addcreated in your project. Checking your git history can help you spot exactly what was added during the installation. - Remove unused peer dependencies:
If the package added extra peer dependencies during installation, uninstall those too if they’re no longer needed for your project.
- Uninstall the core package via your package manager:
Verify and finalize
After making these changes, runnpm install(oryarn install) to sync your dependency tree, then test your project to ensure everything runs smoothly without the removed package.
It’s worth noting that the Angular team has discussed adding official ng remove functionality to pair with ng add, but it hasn’t been fully rolled out for all packages yet.
内容的提问来源于stack exchange,提问作者Dmitry Grinko

