简体   繁体   English

angular ng serve 命令抛出错误:发生未处理的异常:项目不存在

[英]angular ng serve command throws error: An unhandled exception occurred: Project does not exist

I am trying to up my local angular dev env using ng serve, it was working perfectly fine a day back, but now every time I run ng serve or npm start it throws an error:我正在尝试使用 ng serve 来启动我的本地 angular dev env,一天前它工作得很好,但现在每次运行 ng serve 或 npm start 时都会抛出一个错误:

An unhandled exception occurred: Project does not exist.发生未处理的异常:项目不存在。

I have tried running ng serve in one of my different project, it works there.我曾尝试在我的一个不同项目中运行 ng serve,它在那里工作。

not sure what it causing this error不知道是什么导致了这个错误

Make sure the name of the project in your angular.json matches what is being called from your package.json scripts section.确保angular.json中的项目名称与从package.json脚本部分调用的名称相匹配。

This is how I discovered the problem.这就是我发现问题的方式。 I was getting the following error:我收到以下错误:

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

When I went to that log file, I saw this:当我去那个日志文件时,我看到了这个:

[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)

So I opened node-modules-architect-host.js:94:23 and saw this:所以我打开node-modules-architect-host.js:94:23看到了这个:

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

I have no idea why they throw an error like this without telling you which project does not exist.我不知道为什么他们会抛出这样的错误而不告诉你哪个项目不存在。 So I changed the file to be like this:所以我把文件改成这样:

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

Now, when I build, I get an error like this instead:现在,当我构建时,我收到了这样的错误:

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

Finally I can see who the culprit is!终于知道谁是罪魁祸首了! A global search for "client-app" showed me that it was being used from package.json , but my project in angular.json was "ClientApp".对“client-app”的全局搜索显示它是从package.json中使用的,但我在angular.json中的项目是“ClientApp”。 Changing all references of "ClientApp" to "client-app" fixed the problem for me.将“ClientApp”的所有引用更改为“client-app”为我解决了这个问题。

Similar to issue mentioned by adam0101, the issue got fixed for me by changing the project name.与adam0101 提到的问题类似,通过更改项目名称为我解决了该问题。 Infact package.json and angular.json names can be different( at least for me), but within angular.json, the naming was consistent事实上 package.json 和 angular.json 名称可以不同(至少对我而言),但在 angular.json 中,命名是一致的

Old老的

  "$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
            }
          }
        },

Modified修改的

 "$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"
            }
          }
        },

Make sure you angular.json have this config确保你的 angular.json 有这个配置

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

please check projects name and defaultProject are same请检查项目名称和 defaultProject 是否相同

testapp测试应用

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

..................... ………………

"defaultProject": "testapp",

please try请试试

npm install 

or要么

yarn install

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

I match the values in angular.json & package.json and my problem was solved我匹配了 angular.json 和 package.json 中的值,我的问题就解决了

angular.json:角度.json:

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

package.json包.json

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

@adam0101 The solution helped a lot! @adam0101 解决方案帮助很大! Thank you!!!谢谢!!!

But I had to add a console.log(target):但我不得不添加一个console.log(target):

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

And I found in the package.json the wrong project name over a global search:我在 package.json 中通过全局搜索发现了错误的项目名称:

失败:

Project name in angular.json must be the same at every property where is needed. angular.json 中的项目名称必须与需要的每个属性相同。

And take good care of letter case, even in paths, at angular.json.并在 angular.json 中妥善处理字母大小写,即使是在路径中。

Windows doesn't complain, but Angular does!! Windows 不会抱怨,但 Angular 会!

For anyone still having this issue.对于仍然有此问题的任何人。 It's likely you didn't run npm install or yarn before calling the run script.在调用运行脚本之前,您可能没有运行npm installyarn

I had the same problem with Angular Universal .我对Angular Universal也有同样的问题。 For me it was the simplest thing.对我来说,这是最简单的事情。 It is npm run dev:ssr not ng run dev:ssr .它是npm run dev:ssr而不是ng run dev:ssr However, it is ng run myproject:serve-ssr .但是,它是ng run myproject:serve-ssr Watch for differences between ng and npm .注意ngnpm之间的差异。

I duplicated a project and changed its name getting me this error.我复制了一个项目并更改了它的名称,导致我出现此错误。 Fixed it by changing the project name everywhere else in angular.json.通过更改 angular.json 中其他地方的项目名称来修复它。

For example例如


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

@Tony Ngo @托尼·恩戈

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ng-num-formatter": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/ng-num-formatter",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              {
                "glob": "**/*",
                "input": "src/assets",
                "output": "assets"
              },
              {
                "glob": "**/*.svg",
                "input": "node_modules/ionicons/dist/ionicons/svg",
                "output": "./svg"
              }
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ng-num-formater:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "ng-num-formater:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ng-num-formater:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "ng-num-formater:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "ng-num-formater:serve:production"
            }
          }
        }
      }
    },
    "jat-userslist": {
      "projectType": "library",
      "root": "projects/jat-userslist",
      "sourceRoot": "projects/jat-userslist/src",
      "prefix": "jux",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "tsConfig": "projects/jat-userslist/tsconfig.lib.json",
            "project": "projects/jat-userslist/ng-package.json"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/jat-userslist/src/test.ts",
            "tsConfig": "projects/jat-userslist/tsconfig.spec.json",
            "karmaConfig": "projects/jat-userslist/karma.conf.js"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "projects/jat-userslist/tsconfig.lib.json",
              "projects/jat-userslist/tsconfig.spec.json"
            ],å
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "ng-num-formatter"
}

this is my angular.json code 这是我的angular.json代码

对我来说,它只能使用

npm run cordova:run android

Make sure the path of "styles" in the angular.json should be确保 angular.json 中“样式”的路径应该是

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

instead of代替

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

And also remove one dot from the "scripts"并从“脚本”中删除一个点

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

If you are working on an existing project, you might need want to check the codeCoverageExclude inside angular.json and see if your *.spec.ts files are defined inside the array.如果您正在处理现有项目,您可能需要检查codeCoverageExclude angular.json并查看您的*.spec.ts文件是否在数组中定义。

You might find your files excluded on the test.您可能会发现测试中排除了您的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ng serve 在 angular 中发生未处理的异常 - An unhandled exception occurred in angular using ng serve 发生未处理的异常:项目“app”不存在。 离子服务后收到此错误 - An unhandled exception occurred: Project "app" does not exist. getting this error after ionic serve Angular cli 显示错误“发生未处理的异常:没有项目支持‘测试’目标。” 运行 ng test 或 ng serve 命令时 - Angular cli showing error "An unhandled exception occurred: No projects support the 'test' target." when running ng test or ng serve command 发生未处理的异常:项目不存在。 Angular SSR - An unhandled exception occurred: Project does not exist. Angular SSR Ng 使用 Angular 10 项目时的构建问题 - [发生未处理的异常:作业名称“..getProjectMetadata”不存在。] - Ng Build issues when using Angular 10 project - [An unhandled exception occurred: Job name “..getProjectMetadata” does not exist.] ng serve 抛出错误“服务命令需要在 Angular 项目中运行,但找不到项目定义” - ng serve throws error "The serve command requires to be run in an Angular project, but a project definition could not be found" 构建 Ionic v6 cordova android angular13 应用程序返回错误“发生未处理的异常:项目目标不存在。” - Building Ionic v6 cordova android angular13 app returns ERROR "An unhandled exception occurred: Project target does not exist." 运行 ng serve 发生未处理的异常 - Unhandled exception occurred running ng serve 我在尝试执行 ng serve 时收到此错误“发生未处理的异常:找不到模块'@angular/compiler-cli' - I Get this error when im trying to execute ng serve "An unhandled exception occurred: Cannot find module '@angular/compiler-cli' 发生未处理的异常:在工作空间中未设置配置&#39;es5&#39;。 ng在角度8中投放es5 - An unhandled exception occurred: Configuration 'es5' is not set in the workspace. ng serve in angular 8 for es5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM