【问题标题】:angular ng serve command throws error: An unhandled exception occurred: Project does not existangular ng serve 命令抛出错误:发生未处理的异常:项目不存在
【发布时间】:2019-12-26 09:18:11
【问题描述】:

我正在尝试使用 ng serve 升级我的本地 angular dev env,它在一天前工作得非常好,但现在每次我运行 ng serve 或 npm start 时都会抛出一个错误:

发生未处理的异常:项目不存在。

我尝试在我的一个不同项目中运行 ng serve,它在那里工作。

不知道是什么导致了这个错误

【问题讨论】:

  • 您是否从正确的目录执行ng serve
  • 更新或重新安装 Angular cli
  • 我有完全相同的错误,更新或重新安装对 angular/cli 和 angular 本身都没有帮助

标签: angular serve


【解决方案1】:

如果您正在处理现有项目,您可能需要检查 codeCoverageExclude 内的 angular.json 并查看您的 *.spec.ts 文件是否在数组中定义。

您可能会发现您的文件在测试中被排除在外。

【讨论】:

    【解决方案2】:

    我复制了一个项目并更改了它的名称,导致我出现此错误。通过在 angular.json 中的其他任何地方更改项目名称来修复它。

    例如

    
        "e2e": {
                  "builder": "@angular-devkit/build-angular:protractor",
                  "options": {
                    "protractorConfig": "e2e/protractor.conf.js",
                    "devServerTarget": "YourNewProjectName:serve"
                  },
                  "configurations": {
                    "production": {
                      "devServerTarget": "YourNewProjectName:serve:production"
                    }
                  }
                },
    
    

    【讨论】:

      【解决方案3】:

      确保 angular.json 中“样式”的路径应该是

      "./node_modules/bootstrap/dist/css/bootstrap.min.css"
      

      而不是

      "../node_modules/bootstrap/dist/css/bootstrap.min.css"
      

      并从“脚本”中删除一个点

       "scripts": [
                    "./node_modules/bootstrap/dist/js/bootstrap.min.js"
                  ]
      

      【讨论】:

        【解决方案4】:

        对我来说,它只适用于

        npm run cordova:run android
        

        【讨论】:

          【解决方案5】:

          请检查项目名称和默认项目是否相同

          测试应用

          {
          "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
          "version": 1,
          "newProjectRoot": "projects",
          "projects": { // here 
              "testapp": {
                  "root": "",
                  "sourceRoot": "src",
                  "projectType": "application",
          

          .....................

          "defaultProject": "testapp",
          

          请尝试

          npm install 
          

          yarn install
          

          【讨论】:

            【解决方案6】:

            Angular Universal 也有同样的问题。对我来说,这是最简单的事情。它是 npm run dev:ssr 而不是 ng run dev:ssr。但是,它是 ng run myproject:serve-ssr。注意 ngnpm 之间的区别。

            【讨论】:

              【解决方案7】:

              angular.json 中的项目名称在需要的每个属性处必须相同。

              并在 angular.json 中妥善处理字母大小写,即使在路径中也是如此。

              Windows 不会抱怨,但 Angular 会抱怨!

              【讨论】:

                【解决方案8】:

                @adam0101 这个解决方案帮了大忙!谢谢!!!

                但我必须添加一个 console.log(target):

                const projectDefinition = this._workspace.projects.get(target.project);
                if (!projectDefinition) {
                    console.log(target)
                    throw new Error('Project does not exist.');
                }
                

                我在 package.json 中通过全局搜索发现了错误的项目名称:

                【讨论】:

                  【解决方案9】:

                  我匹配了 angular.json 和 package.json 中的值,我的问题就解决了

                  angular.json:

                  "projects": {
                      "step1": {
                        "projectType": "application",
                        ...,
                        ...,
                  

                  package.json

                  {
                    "name": "step1",
                    ...,
                    ...,
                  

                  【讨论】:

                    【解决方案10】:

                    与 adam0101 提到的问题类似, 通过更改项目名称为我解决了这个问题。事实上 package.json 和 angular.json 名称可以不同(至少对我而言),但在 angular.json 中,命名是一致的

                      "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
                      "version": 1,
                      "newProjectRoot": "projects",
                      "projects": {
                        "e-admin": {   ======NAME USED HERE=======
                          "root": "",
                    ...
                    "serve": {
                              "builder": "@angular-devkit/build-angular:dev-server",
                              "options": {
                                "browserTarget": "eAdmin:build"    ===NAME USED HERE====
                              },
                              "configurations": {
                                "production": {
                                  "browserTarget": "eAdmin:build:production"  <===NAME USED HERE
                                }
                              }
                            },
                    

                    修改

                     "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
                      "version": 1,
                      "newProjectRoot": "projects",
                      "projects": {
                        "e-admin": {
                          "root": "",
                    ...
                    "serve": {
                              "builder": "@angular-devkit/build-angular:dev-server",
                              "options": {
                                "browserTarget": "e-admin:build"
                              },
                              "configurations": {
                                "production": {
                                  "browserTarget": "e-admin:build:production"
                                }
                              }
                            },
                    

                    【讨论】:

                    • 谢谢,您的回答帮助我不要浪费时间,
                    • 同意,名称可以不同。实际上,您可能在 angular.json 中定义了一个命令,但即使在 package.json 中不存在,它仍然可以工作。
                    【解决方案11】:

                    我修复了这个问题,将 tsconfig.json 中的目标从 es2015 更改为 es5

                    【讨论】:

                    • 如果您在没有添加或删除任何模块的情况下突然报告问题.....这种方法将工作大拇指!
                    【解决方案12】:

                    对于仍然遇到此问题的任何人。很可能您在调用运行脚本之前没有运行npm installyarn

                    【讨论】:

                      【解决方案13】:

                      确保angular.json 中的项目名称与package.json 脚本部分中调用的名称相匹配。

                      这就是我发现问题的方式。我收到以下错误:

                      An unhandled exception occurred: Project does not exist.
                      See "C:\Users\Adam\AppData\Local\Temp\ng-XcQsPs\angular-errors.log" for further details.
                      

                      当我访问那个日志文件时,我看到了这个:

                      [error] Error: Project does not exist.
                          at WorkspaceNodeModulesArchitectHost.findProjectTarget (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular-devkit\architect\node\node-modules-architect-host.js:94:23)
                          at WorkspaceNodeModulesArchitectHost.getBuilderNameForTarget (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular-devkit\architect\node\node-modules-architect-host.js:13:39)
                          at RunCommand.runSingleTarget (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular\cli\models\architect-command.js:175:55)
                          at RunCommand.runArchitectTarget (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular\cli\models\architect-command.js:218:35)
                          at RunCommand.run (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular\cli\commands\run-impl.js:14:25)
                          at RunCommand.validateAndRun (C:\Users\Adam\source\repos\TechScore.Express\node_modules\@angular\cli\models\command.js:134:39)
                          at process._tickCallback (internal/process/next_tick.js:68:7)
                          at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
                          at startup (internal/bootstrap/node.js:283:19)
                          at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
                      

                      于是我打开node-modules-architect-host.js:94:23 看到了这个:

                      const projectDefinition = this._workspace.projects.get(target.project);
                      if (!projectDefinition) {
                          throw new Error('Project does not exist.');
                      }
                      

                      我不知道他们为什么会抛出这样的错误而不告诉你哪个项目不存在。所以我把文件改成这样:

                      throw new Error('Project does not exist. ' + target.project);
                      

                      现在,当我构建时,我收到了这样的错误:

                      An unhandled exception occurred: Project does not exist. client-app
                      

                      我终于知道谁是罪魁祸首了!对“client-app”的全局搜索显示它是从package.json 使用的,但我在angular.json 中的项目是“ClientApp”。将“ClientApp”的所有引用更改为“client-app”为我解决了这个问题。

                      【讨论】:

                      • 这里应该加了('Project does not exist. ' + target.project);这个小块,这样可以节省很多时间。
                      • 这是一个很好的解决方案和解释!谢谢
                      • 当我根据您的建议更新我的 node-modules-architect-host.js 文件时,我什至没有收到新的错误消息?任何想法怎么会发生?
                      • @dfbenny,我会仔细检查是否修改了正确的文件,修改后文件没有被旧版本覆盖,并且没有使用缓存版本(也许很清楚包缓存?)。
                      • 自 2020 年 8 月起,项目名称现已包含在 angular-devkit-architect-builds 中。
                      【解决方案14】:

                      确保你的 angular.json 有这个配置

                      {
                          "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
                          "version": 1,
                          "newProjectRoot": "projects",
                          "projects": { // here 
                              "testapp": {
                                  "root": "",
                                  "sourceRoot": "src",
                                  "projectType": "application",
                      

                      【讨论】:

                      • 此应用程序包含我正在构建的自定义 ng 库,
                      猜你喜欢
                      • 1970-01-01
                      • 2020-07-08
                      • 1970-01-01
                      • 2022-07-20
                      • 2020-12-29
                      • 2021-09-04
                      • 2019-07-02
                      • 2017-03-07
                      • 2022-06-22
                      相关资源
                      最近更新 更多