ng: command not found

时间:2018-07-25 05:09:53

标签: angular angular-cli

I am trying to run ng build in my project folder, but I get the following response:

bash: ng: command not found

What am I missing? Does it have to do with admin privileges, my path/profile, or something I need to install globally?

This is my package.json:

{
"name": "meanauthapp",
"version": "1.0.0",
"description": "MEAN stack authentication app",
"main": "app.js",
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "4.16.2",
"mongoose": "4.13.6",
"bcryptjs": "2.4.3",
"cors": "2.8.4",
"jsonwebtoken": "8.1.0",
"body-parser": "1.18.2",
"passport": "0.4.0",
"passport-jwt": "3.0.1"
},
"author": "",
"license": "MIT",
"devDependencies": {
  "@angular/cli": "^1.6.0"
  }
}

12 个答案:

答案 0 :(得分:3)

万一其他人遇到了这个问题,这就是我在MacOS上系统范围内解决此问题的方法。希望这会有所帮助。

验证节点已安装:

$ node -v
v11.2.0

验证已安装npm:

$ npm -v
6.4.1

验证您的npm全局安装文件路径是否已配置(称为prefix)。我的在〜/ .npm-packages下:

$ npm config ls -l | grep prefix
prefix = "/Users/christiangroleau/.npm-packages"

如果没有,您可以将其放入〜/ .npmrc文件中:

echo prefix=~/.npm-packages >> ~/.npmrc

验证您的前缀路径已在系统PATH中列出:

$ echo $PATH
/Users/christiangroleau/.npm-packages/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

如果没有,请调用以下内容:

export PATH="$HOME/.npm-packages/bin:$PATH"

最后,您可以重新安装angular-cli(就我而言,我需要将其全局安装):

$ npm install -g @angular/cli

验证安装:

$ ng -v

答案 1 :(得分:2)

尝试卸载并清理缓存。

npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli

答案 2 :(得分:1)

尝试一下:

node_modules/.bin/ng build

或者更好的方法是,在package.json的"ng": "ng"键中添加空白的scripts行,并运行以下命令:

`npm run ng build`

您还可以将其添加到您的路径中:

export PATH=$PATH:./node_modules/bin

这会让您在可能拥有的任何npm项目中运行任何二进制文件。

答案 3 :(得分:1)

npm无法识别ng命令。

尝试以下命令:

npm link @angular/cli

链接npmangular-cli

答案 4 :(得分:1)

对于Windows用户:只需转到Windows环境设置并将用户路径设置为C:\ Users \ HP \ AppData \ Roaming \ npm

答案 5 :(得分:0)

您需要全局安装Angular CLI。 运行npm install -g @angular/cli

答案 6 :(得分:0)

根据您对其他答案的评论:This npm issue可能与您的问题有关,您的npm安装可能已过时。

首先检查是否使用最新版本的节点。然后根据问题提示,使用npm i -g npm更新npm(在底部附近)。然后,如果仍然找不到ng,请遵循update instructions of @angular/cli

npm uninstall -g @angular/cli
npm cache verify
# if npm version is < 5 then use 'npm cache clean'
npm install -g @angular/cli@latest

答案 7 :(得分:0)

这很麻烦,所以这是我的解决方法

1. npm install -g @angular/cli

2. Make sure the ng path is correct
    * cd ~/npm-global/bin
    * ls and make sure ng exists here!
3. create .bashrc or .bash_profile or .bash_everything file on your home directory with:
    * touch ~/.bash_profile 
open this file with text editor or Sublime or vi or etc.
    * open -e .bash_profile 
4. Add ng as alias
    * writing this:
   alias ng="~/npm-global/bin/ng"  or   alias ng="MyFullPAThere/npm-global/bin/ng"
    * save file and close text editor. 
5. Temporarily Update . bash_profile reference
Your terminal wont take bash updates until a restart but the following command will let you use the updates during the terminal session:
    * source ~/.bash_profile
6. Check that ng works
    * ng --version

希望对某人有帮助!

答案 8 :(得分:0)

如果您在本地安装了依赖项,则需要引用这些软件包而不是全局软件包。在命令前使用npx,例如:npx ng build引用本地安装的软件包。 ng build是指全局软件包。

答案 9 :(得分:0)

我遇到了同样的错误,但是我正在运行Windows 10,但没有任何答案适合我。这些是我采取的解决措施:

通过在Git BASH中运行以下命令,

确认已安装Angular

npm run ng -v

安装Angular (如果尚未安装),请运行以下命令:

npm install -g @angular/cli@latest

通过运行以下命令获取npm路径

npm config get prefix

打开高级系统设置,方法是使用快捷键Windows +暂停(也称为中断)并单击高级系统设置或使用Cortana搜索短语< em>系统环境变量

单击环境变量以打开环境变量对话框。

为用户变量 Path 选择行,然后单击“编辑”

单击“新建” ,然后输入先前返回的路径。然后单击“确定”,然后再次单击“确定”以关闭“环境变量”对话框。

重新启动Git BASH 并通过运行以下命令测试其是否正常运行:

ng -v

答案 10 :(得分:0)

就我而言,我刚刚将整个存储库(包括其node_modules)从Windows计算机复制到Linux计算机,并且运行npx ng XXX失败,并出现以下错误:

npx: installed 1 in 1.677s
command not found: ng

当然,主要问题是换行符。使用dos2unix包,我能够克服它:

sudo apt install -y dos2unix
dos2unix node_modules/.bin/ng
chmod +x node_modules/.bin/ng

答案 11 :(得分:0)

如果您使用的是 mac 并使用 nvm,那么只需将您的 nvm dir 节点版本作为别名 像这样

alias ng=/Users/your-user/.nvm/versions/node/v14.16.0/lib/node_modules/@angular/cli/bin/ng