繁体   English   中英

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

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

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

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

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

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

确保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”为我解决了这个问题。

与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 有这个配置

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

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

测试应用

{
"$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

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

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

角度.json:

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

包.json

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

@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 中通过全局搜索发现了错误的项目名称:

失败:

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

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

Windows 不会抱怨,但 Angular 会!

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

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

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

例如


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

@托尼·恩戈

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

这是我的angular.json代码

对我来说,它只能使用

npm run cordova:run android

确保 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"
            ]

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

您可能会发现测试中排除了您的文件。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM