Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found

Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found

当 运行 终端命令 ng serverng serve --live-reload=true 时,我遇到了这个问题:

The serve command requires to be run in an Angular project, but a project definition could not be found.

我也遇到了这个问题,并通过以下命令 运行 解决了。

ng update @angular/cli --migrate-only --from=<WhateverVersionYouAreCurrentlyOn>

例如

ng update @angular/cli --migrate-only --from=1.7.3

正在从 here

获取参考

确保你是运行应用程序根文件夹中的命令..

另外请确保如果您运行 ng 发球,请先停止它...

已解决。我在 ubuntu 18.04.01 LTS 上遇到了同样的问题。以上选项均无效,因为我使用的是 NVM(节点版本管理器。) 解决方案是
在创建项目之前,使用命令
nvm 使用 10
(我的节点版本是10.15.0,所以我用了"nvm use 10")
它会告诉你输出-
现在使用节点 v10.15.0 (npm v6.4.1)
版本会因各个系统而异。 然后创建新项目。

我遇到了同样的错误。原因是新的 angular cli 更新使 angular-cli.json 变得多余,它被替换为 angular.json。我之前的 Angular Cli 版本是 1.7.4,所以要进行更改,我 运行 下面的命令,它会为你进行转换:

ng update @angular/cli --migrate-only --from=1.7.4

您可以使用命令 'ng update --all' 更新 angular。这需要时间,但请确保您的所有组件都是最新的。

确保您已在项目中设置新的 angular 版本配置。新的 angular cli 使用 angular.json 而不是 .angular-cli.json 进行配置。

关注 migration guide.

我遇到过同样的问题,

我使用旧的 angular-cli 版本 (1.4.7) 创建了新的 angular 项目 然后我使用以下命令更新了 angular-cli(不要执行以下操作来更新 CLI

  • npm uninstall -g @angular/cli
  • npm cache clean --force
  • npm install -g @angular/cli@latest

现在当我尝试 ng serve 时,我遇到了和你一样的错误

Angular Cli Error: The serve command requires to be run in an Angular project, but a project definition could not be found

执行以下操作以更新 CLI

ng update @angular/cli --migrate-only --from=1.4.7

如果你已经下载了项目,在项目中执行

npm install

在您的程序包管理器控制台中 运行 命令

PM> cd NameofApp 

那么你可以尝试 运行 命令 there.For 实例

PM > ng generate component Home

当您尝试 运行 Angular 命令 "ng s" 或 "ng serve" 其他文件夹而不是 Angular 的根文件夹时,会发生这种情况申请。

同时使用以下命令升级 Angular:

ng update @angular/cli --migrate-only --from=1.7.4

这会解决问题。

请检查您是否在项目文件夹中,然后再次尝试命令 ng serve

自从我在我的项目之外尝试 ng serve 命令后,我遇到了同样的错误

例子:: 假设您有一个名为 "Ecommerce" 的项目遍历文件夹并尝试命令 ng serve 打开终端 cd电子商务 服务

最后,下面的命令为我解决了这个问题!

ng update --all --force

就我而言,我忘记更改目录,所以在 运行 命令之后:

ng new AngularProject

执行另一个命令:

cd AngularProject

ng serve对我有用。

我在使用 Angular7 时遇到了同样的问题,因为我们需要在 运行 您的应用程序之前进入根文件夹。转到您的应用程序的根文件夹和 运行 命令。它非常适合我。

当我们出错时可能会出现此错误directory.so 使用cd 命令更改您的目录。 从上面的评论中看不出是哪个特定命令给出的 这个错误所以这个答案是基于假设你是 运行ning ng serve 在根 folder/actual 项目文件夹之外。这就是它给出错误的原因,因为 cli 命令需要 conf 和构建文件才能 运行 cli 构建。

These should be the steps:
npm install -g @angular/cli  //corrected from 'angular-cli'
ng new projectname
cd projectname
ng serve
open http://localhost:4200

我遇到了同样的问题,但是我按照这些代码步骤解决了。

Step 1) npm install -g @angular/cli
Step 2) ng new my-angular-project
Step 3) cd my-angular-project
Step 4) ng serve --open

正如乌斯曼所说,我第一次遇到了很多麻烦,我正在学习教程。我没有将目录设置为项目目录,因此出现此错误。

我通过

解决了这个问题
  1. 将正确的根目录设置为我的项目所在的位置 cd myproject
  2. 编译应用程序 - ng serve

就是这样。

当我尝试更新到最新的 nodenpm 包时,它发生在我现有的项目中:

  1. 全局卸载:npm uninstall -g angular-cli
  2. 重置缓存:npm cache cleannpm cache verify(对于 npm version > 5)
  3. 安装最新版本:npm install -g @angular/cli@latest

Angular Cli Error: The serve command requires to be run in an Angular project, but a project definition could not be found

Problem was missing angular.json files.

    ng update --all --force

在 Angular 7+

中测试

这解决了我的问题:

ng generate component nameComponent --module app.module

发生这种情况是因为我们在其他一些地方点击了 ng serve 命令 path.This 可以通过在我们的项目所在的路径上点击 ng serve 或 npm start 命令来解决(获取路径直到包含的文件夹来源,node_modules,等等)

F:\project\AngularDemo\AngularDemoapp> 服务

当您正在 运行 的项目不是 angular 项目时,会发生此错误。 虽然您已经下载了一个 angular 项目,但还没有安装所有依赖项,这就是为什么 ng serve 命令对您不可用的原因。

只需导航到存储项目的路径并使用命令

npm install

(注意 - Node.js 应该安装在您的系统中,如果您使用 Angular 2 或更高版本 angular cli 也应该在您的系统中安装 运行 这个命令。 检查你的系统是否安装了 node.js 1)打开cmd(任何路径-作为节点应该全局安装在您的系统中) 2) 使用命令

node -v
npm -v

获取节点和 npm 版本)

还有一件重要的事: 如果您系统中安装的版本高于项目所需的版本,angular cli 版本不会有任何区别。 它会给出警告,但您可以忽略该警告。

上面的都试过了但是对我来说解决了

npm start

确保您已使用以下命令创建 angular 应用程序,然后 运行 在创建的项目文件夹中使用 ng 服务。

ng new Project_Name

我当时很傻,但需要发表评论,也许我可以为其他人省去一些麻烦。 如果您 执行多项任务并且有多个项目,则会出现相同的消息,因此某些项目嵌套在项目文件夹中,例如 projectFolder/subProject/projectOne 所以我在 projectFolder/subProject 尝试 运行 命令 ng serve

确保您在正确的文件夹中,因为如果您在错误的文件夹中并且缺少 conf 和 build 也会发生同样的错误!

当我没有安装 angular 核心时发生在我身上:

Angular CLI: 9.0.0-rc.0
Node: 11.12.0
OS: darwin x64
Angular: undefined
...

所以我运行

npm i @angular/core

这给了我 ng -v

Angular CLI: 9.0.0-rc.0
Node: 11.12.0
OS: darwin x64
Angular: 8.2.13
... core

那我运行

ng update @angular/cli --migrate-only --from=8.2.13

已删除 node_modules

npm install
cd <project name>
ng serve

成功了!

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ ** ℹ 「wdm」: Compiled successfully.

我建议确认你是否在项目目录中。

我正在使用 ionciv1,在文件 ionic.config.json 中删除 angular 类型键并且它工作正常

示例:

  {
  "name": "nombre",
  "integrations": {
    "cordova": {}
  },
  "type": "angular", // delete
  "gulpStartupTasks": [
    "sass",
    "templatecache",
    "ng_annotate",
    "useref",
    "watch"
  ],
  "watchPatterns": [
    "www/**/*",
    "!www/lib/**/*"
  ],
  "browsers": [
    {
      "platform": "android",
      "browser": "crosswalk",
      "version": "12.41.296.5"
    }
  ],
  "id": "0.1"
}

已解决

     {
      "name": "nombre",
      "integrations": {
        "cordova": {}
      },
      "gulpStartupTasks": [
        "sass",
        "templatecache",
        "ng_annotate",
        "useref",
        "watch"
      ],
      "watchPatterns": [
        "www/**/*",
        "!www/lib/**/*"
      ],
      "browsers": [
        {
          "platform": "android",
          "browser": "crosswalk",
          "version": "12.41.296.5"
        }
      ],
      "id": "0.1"
    }

重新安装应用程序比更新或编辑一些 npm 无法识别的配置要好得多,因为缺少配置或者我们不确定哪里出了问题。

我选择@Tinu 解决方案有效

步骤 1) npm install -g @angular/cli

步骤 2) 新建 my-angular-project

步骤 3) cd my-angular-project

步骤 4) ng serve --open

刚刚备份了我们当前的源代码,并将其放回新创建的 angular 项目中。

我在使用 VS Code 时遇到了同样的问题,事实证明启用 Angular 图标检测可以解决问题

切换到您的应用程序文件夹,然后运行再次生成命令:

此错误通常可以追溯到更新我们的全局或本地 CLI 运行时间。要检查这是否是问题所在,我们需要查看 package.json 文件。在那里,我们应该寻找 @angular/cli 依赖项。这应该指示用于创建项目的 CLI 版本。让我们记下这个值,因为我们需要用它来稍后迁移我们的项目。

我们现在应该通过在终端 window.

上输入以下命令来与当前 CLI 运行时间进行比较
ng v

If you get error An unhandled exception occurred: ENOENT: no such file or directory, scandir '~/your-app/node_modules', then you forgot to run npm install

控制台应显示当前的 CLI 版本。如果版本不同,我们需要通过运行以下命令将项目迁移到新的 CLI 版本。请注意,from 版本参数应设置为 @angular/cli 设置中找到的值。

ng update @angular/cli --migrate-only --from=1.6.7

我们现在应该看看我们的项目,并且应该注意到 package.json 文件中 CLI 依赖版本的变化。此外,根据您的 CLI 版本,删除 .angular-cli.json 文件,并创建一个新的 angular.json 文件。这是新 CLI 正在寻找的项目文件,因此是找不到项目错误的来源。通过添加此文件,我们应该能够再次回到 运行 我们项目的正轨。

我们应该能够输入以下命令并且项目应该可以正常加载。

ng server

感谢ozkary

ng update --all --force都不行?

可能只是当您从 .angular-cli.json(例如 Angular 4)的版本执行 Angular 升级时,它已被用于 [=14] 的较新版本之一=].在这种情况下,您可能需要删除 .angular-cli.json 并替换为等效的元数据 angular.json。之后,您将能够通过一个命令或逐步执行更新。

本项目不使用angularcli,直接使用webpack。要启动 运行 命令

npm start

您的 angular.jason 文件中的任何问题都可能导致此错误。因此,如果文件在您的目录中丢失或者它有可能发生的错误。

它一直给我同样的错误。我尝试了此页面上的所有命令但无济于事。我把电脑关了又开。它对我有用。

我碰巧在 Rails 上用 Ruby 创建的项目中尝试执行 ng serve 命令。换句话说,我必须 运行 rails -s 命令 Ruby on Rails。 我很困惑,因为 Angular 内置于 Rails 的 Ruby 中,所以我不得不 运行 rails -s命令启动我的项目的本地服务器,它没有问题。

试试 --> npx ng serve

我遇到了同样的错误 运行 ng serve 我已经修复了它 运行 npx ng serve

我从一个主题示例项目库开始。这是angular.json

 "projects": {
    "<project name>": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
    ...

它还有

 "defaultProject": "<project name>",
  "cli": {
    "analytics": false
  }

将项目名称与此匹配解决了我在 Angular 13.

上的问题

我也有同样的错误。所以,我首先去了根文件夹(包含 angular.json 文件的文件夹)然后

run "npm install" command(creating node modules),
then run "npm audit fix --force"
and then run "ng build",(building project)
and "ng serve"  (runnning project)

这 3 个命令帮助了我 npm 安装 --force npm 审计修复 --force npm 开始

我在 Angular 应用程序的父目录中弄错了 运行 ng 服务器,OP 得到了答案,但如果有人正在搜索答案,可能也想 cd 到应用程序目录中.