“ng”未被识别为内部或外部命令

‘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如果不存在则添加命令输出的路径。

如果将命令的输出添加到 PATH 环境变量,则必须重新启动所有打开的命令提示符才能生效。

在 Windows 上,该npm config get prefix命令的输出类似于:C:\Users\Your_User_Name\AppData\Roaming\npm

要在 Windows 机器上更新 PATH,您必须:

  1. 打开开始搜索并输入env然后点击“编辑系统环境变量”
  2. 然后点击“环境变量”
  3. 编辑Path变量并添加从命令获得的输出
    npm config get prefix

路径应该看起来像C:\Users\Your_User_Name\AppData\Roaming\npm(确保Your_User_name用您的实际用户名替换占位符)。

如果将命令的输出添加到 PATH 环境变量,则必须重新启动所有打开的命令提示符才能生效。

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.

shell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Make sure to open your PowerShell as an administrator before you run the 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:

shell
# 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.

~/.bashrc
# 👇️ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
If you add the output from the command to your PATH environment variable, you have to restart any open command prompts before it takes effect.

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.

During the installation, you might get a prompt for whether you want to automatically update the PATH environment variable on your system, make sure to tick the option.
shell
# 👇️ 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
If the global installation of Angular fails, you have to open your shell as an administrator or run the command prefixed with sudo.
shell
# 👇️ 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: