‘ng’ 不被识别为内部或外部命令
‘ng’ is not recognized as an internal or external command
要解决“ng is not recognized as an internal or external command, opable program or batch file”的错误,通过运行全局安装Angular CLI
npm install -g @angular/cli@latest
并确保您的PATH环境变量设置正确。
打开终端并
通过运行以下命令全局安装Angular cli 。
# 👇️ install angular cli globally npm install -g @angular/cli@latest # 👇️ get package version ng version # 👇️ create workspace ng new my-project # 👇️ run the application cd my-project ng serve
如果全局安装Angular
失败,您必须以管理员身份打开 shell 或运行前缀为
sudo 的命令。
# 👇️ If you got permissions error, run with sudo sudo npm install -g @angular/cli@latest ng version ng new my-project cd my-project ng serve
package.json
您可以通过在项目的根目录(文件所在的位置)中打开终端并运行命令,将您的项目链接到全局安装的 Angular 包npm link @angular/cli
。
npm link @angular/cli
npm link命令创建一个从全局安装包到node_modules/
当前文件夹目录的符号链接。
在 Windows 上设置正确的 NPM PATH
如果这没有帮助,请运行以下命令:
npm config get prefix
该命令将向您显示npm放置全局安装包的路径
。全局包将位于bin
指定路径的目录中。
查看操作系统上的 PATH 环境变量,npm config get prefix
如果不存在则添加命令输出的路径。
在 Windows 上,该npm config get prefix
命令的输出类似于:C:\Users\Your_User_Name\AppData\Roaming\npm
。
要在 Windows 机器上更新 PATH,您必须:
- 打开开始搜索并输入
env
然后点击“编辑系统环境变量” - 然后点击“环境变量”
- 编辑
Path
变量并添加从命令获得的输出
npm config get prefix
。
路径应该看起来像C:\Users\Your_User_Name\AppData\Roaming\npm
(确保Your_User_name
用您的实际用户名替换占位符)。
If you get the error “ng cannot be loaded because running scripts is disabled
on this system”, open your PowerShell as an administrator and set its
execution policy with the
Set-ExecutionPolicy
command.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Set-ExecutionPolicy
command.This effectively removes the execution policy of Restricted
, which doesn’t
allow us to load configuration files or run scripts. The Restricted
execution
policy is the default for Windows client computers.
If you are on macOS
, you can update your path with the following command:
# make sure path matches with npm config get prefix export PATH=/usr/local/share/npm/bin:$PATH
If you are on Linux
, you can add the output from the npm config get prefix
command to your .bashrc
file.
# 👇️ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
If that doesn’t help try to reinstall Node.js on your
machine and then install the angular CLI globally by running
npm install -g @angular/cli@latest
.
# 👇️ install angular cli globally npm install -g @angular/cli@latest # 👇️ get package version ng version # 👇️ create workspace ng new my-project # 👇️ run the application cd my-project ng serve
Angular
fails, you have to open your shell as an administrator or run the command prefixed with sudo
.# 👇️ If you got permissions error, run with sudo sudo npm install -g @angular/cli@latest ng version ng new my-project cd my-project ng serve
Alternatively, you can see how you can fix the permissions error on this page in
the
official npm docs.
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials:
- ‘go’ is not recognized as an internal or external command
- ‘next’ is not recognized as an internal or external command
- ‘node’ is not recognized as an internal or external command
- ‘cross-env’ is not recognized as an internal or external command
- ‘expo’ is not recognized as an internal or external command
- ‘nodemon’ is not recognized as an internal or external command
- ‘nuxt’ is not recognized as an internal or external command
- ‘ionic’ is not recognized as an internal or external command
- ‘vite’ is not recognized as an internal or external command