'ng serve' 双重安装后不起作用

'ng serve' does not work after a double install

我曾经有 Angular CLI,但我错误地用

重新安装了它
npm install -g angular-cli

现在当我 运行 ng serve 它抱怨:

It seems like you're using a project generated using an old version of the Angular CLI.

The latest CLI now uses webpack and includes a lot of improvements, include a simpler workflow, a faster build and smaller bundles.

To get more info, including a step-by-step guide to upgrade the CLI, follow this link: https://github.com/angular/angular-cli/wiki/Upgrading-from-Beta.10-to-Beta.12

当我尝试在另一个目录中使用 ng new 创建一个新项目时,它会抱怨:

Cannot find module 'reflect-metadata'

那么问题是什么?

我认为您可能使用的是旧版本的 Angular CLI。您可以通过以下命令重新安装最新版本。

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

关键是 globallocal Angular-cli 版本相同。

假设我们想将其固定到例如。 1.0.0-beta.15.

因此您可以尝试固定 npm install -g angular-cli@1.0.0-beta.15 中的版本,并在您的 package.json 中将其固定为同一版本:"angular-cli": "1.0.0-beta.15"。然后运行npm install --save-dev.

目前 Angular CLI 可以在 @angular/cli 而不是 angular-cli 下的 NPM 上找到。以下是如何使用 uninstall old and install the latest 版 CLI 的文档链接。

我也遇到过,现在解决了:

将此行放入文件 package.json 以供 start:

"start": "ng serve --host 0.0.0.0 --port 4201"

我用过 ng serve --open,它对我有用。

尝试命令

npm start ng s

npm start

我几乎什么都试过了,但这个对我有用:

转到:

C:\Users\<profile_name>\AppData\Roaming\npm\node_modules\@angular\cli\bin\ng

别忘了 ng 最后

然后尝试:

ng -v(显示ng的版本)

Ubuntu,首先做:

npm install --save-dev

尝试:

sudo npm install -g @angular/cli

然后尝试:

ng serve

这将 100% 有效。

npm install -g @angular/cli
cd "yourProjectName"
ng serve -o

试试下面的代码。这对我有用。

npm uninstall -g angular-cli
npm install --cache/tmp/empty-cache
ng serve

就我而言,

我使用的是更高版本的 Angular (8+),它与 ​​Node.js 版本 v8.11.3 不兼容。我什至得到了退出状态代码 3。所以我使用 Windows MSI 安装程序安装了它,它升级了我的 Node.js.

版本

礼貌 - How can I update npm on Windows?.

我遇到了同样的问题。我用的是 Linux 机器,我用 sudo

解决了这个问题

只需在 ng serve 命令前添加 sudo。例如:

sudo ng serve --port 6161

切换到您的项目并尝试此代码:

npm i https://github.com/Qix-/node-error-ex <br/>
npm install <br/>
ng serve --open 

Ubuntu中,按照以下步骤操作:

sudo npm install --save-dev

sudo npm install -g @angular/cli

ng serve

这绝对有效。

对我有用

  1. 使用 Live Server 打开(如果在 Index.html 或 app.component.html 上工作)
  2. 键入 ng serve --open(在 VS 代码终端中)

更改将在 http://localhost:4200/

上显示

如果您使用的是 VS Code,则在 VS Code 中打开整个文件夹。

Code runner 不工作 - “不支持或定义代码语言”

希望对你有帮助:)