Angular CLI无法创建组件:执行命令报NullLogger未定义错误
Hey there, sorry you're hitting this annoying error when trying to generate a component with Angular CLI. Let's break down what's causing this and how to fix it step by step:
This error typically stems from mismatched Angular CLI versions (global vs local), corrupted dependencies, or a messed-up CLI cache. Here are the most reliable fixes to get you back on track:
1. Sync up global and local Angular CLI versions
Version mismatches are the #1 culprit for this kind of weird runtime error. Let's align everything:
- First, uninstall the outdated global CLI:
npm uninstall -g @angular/cli - Clear out any corrupted cache files:
npm cache clean --force - Reinstall the latest stable global CLI:
npm install -g @angular/cli@latest - Now update the local CLI package in your project directory:
npm install @angular/cli@latest --save-dev - Run the update schematic to sync all related Angular packages:
ng update @angular/cli @angular/core
2. Do a clean reinstall of dependencies
Corrupted node_modules or lock files can also trigger this error. Let's start fresh:
- Delete the
node_modulesfolder from your project root. - Delete
package-lock.json(oryarn.lockif you use Yarn). - Reinstall all dependencies:
npm install - Try generating your component again:
ng generate component your-component-name
3. Clear the Angular CLI cache
Sometimes the CLI's local cache gets corrupted. Wipe it with:
ng cache clean --force
Then retry your component generation command.
4. Verify Node.js compatibility
Make sure your Node.js version plays nice with your Angular version. As a general rule:
- Angular 15+ requires Node.js 16.14 or later
- Older Angular versions have specific supported Node ranges (double-check if you're on an older project)
Check your current Node version with:
node -v
Update Node.js to a compatible version if needed.
内容的提问来源于stack exchange,提问作者Brahim Mahjoubi

