Windows环境下Angular 2-4无法运行的CLI安装故障求助
Hey there, let's work through this problem step by step—Windows updates love to mess with permissions and environment paths, which is almost certainly what's going on here.
Step 1: Fix Permission Errors First
The EPERM error and "run as administrator" hint tells us we need elevated privileges to fix the CLI installation. Here's what to do:
- Open Command Prompt as Administrator: Right-click the Command Prompt icon and select Run as administrator (this is non-negotiable for modifying global npm packages on Windows 7).
- Uninstall the broken global Angular CLI installation:
npm uninstall -g @angular/cli - Clear npm's cache to get rid of corrupted files:
npm cache clean --force - Manually delete any leftover
@angular/clifolder: Navigate toC:\Users\<Your Username>\AppData\Roaming\npm\node_modulesand delete the@angular/clidirectory if it's still there (the error about renaming means this folder was locked or corrupted).
Step 2: Reinstall Angular CLI Globally
With the old installation cleaned up, reinstall the latest CLI with admin rights:
npm install -g @angular/cli@latest
This should run without EPERM errors now that you're using an admin prompt and cleared the cache.
Step 3: Fix the "ng is not recognized" Error
This happens when Windows can't find the ng executable in your system's PATH. Let's fix that:
- Locate npm's global bin folder: It's usually at
C:\Users\<Your Username>\AppData\Roaming\npm(this is whereng.cmdgets installed). - Add this path to your system environment variables:
- Right-click Computer → Properties → Advanced System Settings → Environment Variables.
- Under System Variables, find the
Pathvariable, click Edit. - Add the npm bin folder path (e.g.,
C:\Users\JohnDoe\AppData\Roaming\npm) to the list.
- Restart Command Prompt (environment changes won't take effect until you do this).
- Test if
ngworks by running:ng --version
Step 4: Correct Your Project Setup Command
Quick note: npm install ng serve is not a valid command—this is why it didn't work. For your Angular project:
- Navigate to your project folder in Command Prompt (you don't need admin rights for this part).
- Install project dependencies first:
npm install - Then start the development server:
ng serve
Step 5: Verify Project Folder Permissions
Since you mentioned node_modules permissions issues, double-check your project folder's access rights:
- Right-click your project folder → Properties → Security.
- Make sure your user account has Full Control permissions for the folder (and all subfolders like node_modules). If not, click Edit to add or adjust these permissions.
After following these steps, your Angular CLI should work just like it did before the Windows update!
内容的提问来源于stack exchange,提问作者user244394

