非管理员权限下,PowerShell中ng/npm命令无法运行的替代方案咨询
Let's tackle both your PowerShell and CMD errors with workarounds that don't require admin rights:
1. Bypass PowerShell Execution Policy Temporarily
You don't need to change the global execution policy. Instead, launch a one-time PowerShell session with a relaxed policy just for your ng command:
powershell -ExecutionPolicy Bypass -Command "ng serve --open"
This creates a temporary session where script execution is allowed, and it won't affect your system's default security settings once the command finishes.
2. Use npx to Run Local Angular CLI
Global ng scripts might be blocked by PowerShell policies, but you can use npx to run the Angular CLI version installed locally in your project. This avoids relying on the global ng entirely:
Navigate to your project directory (where angular.json is located) and run:
npx @angular/cli serve --open
npx will automatically use the CLI from your project's node_modules folder, which bypasses both the PowerShell policy issue and the strange CMD error you saw.
3. Run via npm Scripts
Another reliable way is to use the npm scripts defined in your project's package.json. Most Angular projects come with a pre-configured serve script, so just run:
npm run serve -- --open
The -- passes the --open flag to the underlying ng serve command. This method doesn't directly call the ng executable, so it sidesteps the PowerShell policy restrictions.
Note on Your CMD Error
The error saying "requires to be run outside of a project" is odd for ng serve (which should run inside a project). This is likely caused by a mismatch between your global Angular CLI version and the one your project uses, or a corrupted global CLI installation. Using the local CLI via npx or npm scripts will resolve this.
内容的提问来源于stack exchange,提问作者Vishesh

