Angular8:ng run =>'项目目标不存在。'使用i18n

时间:2019-07-19 11:22:50

标签: angular angular8 angular-i18n

基于这些教程: https://angular-templates.io/tutorials/about/angular-internationalization-i18n-multi-language-apphttps://medium.com/@ismaestro/angular-7-example-app-with-angularcli-angular-universal-i18n-official-firebase-66deac2dc31e 我正在尝试使用i18n作为国际化系统来构建和使用Angular 8应用程序。

但是,当我尝试运行npm build:ssr(见下文)时,我得到了An unhandled exception occurred: Project target does not exist.

在这里,angular.json:

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
        "boilerplate": {
            "root": "",
            "sourceRoot": "src",
            "projectType": "application",
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-angular:browser",
                    "options": {
                        "outputPath": "dist",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "tsConfig": "tsconfig.app.json",
                        "polyfills": "src/polyfills.ts",
                        "assets": [
                            "src/assets",
                            "src/favicon.ico"
                        ],
                        "styles": [
                            "src/styles.scss"
                        ],
                        "scripts": []
                    },
                    "configurations": {
                        "en": {
                            "outputPath": "dist/browser/",
                            "baseHref": "/",
                            "i18nFile": "src/locale/messages.en.xlf",
                            "i18nFormat": "xlf",
                            "i18nLocale": "en",
                            "i18nMissingTranslation": "error",
                            "assets": [
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/",
                                    "output": "/assets/",
                                    "ignore": [
                                        "base/*",
                                        "css/*",
                                        "js/*"
                                    ]
                                },
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/base",
                                    "output": "/"
                                }
                            ]
                        },
                        "fr": {
                            "outputPath": "dist/browser/",
                            "baseHref": "/fr/",
                            "i18nFile": "src/locale/messages.fr.xlf",
                            "i18nFormat": "xlf",
                            "i18nLocale": "fr",
                            "i18nMissingTranslation": "error",
                            "assets": [
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/",
                                    "output": "/assets/",
                                    "ignore": [
                                        "base/*",
                                        "css/*",
                                        "js/*"
                                    ]
                                },
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/base",
                                    "output": "/"
                                }
                            ]
                        },
                        "production-en": {
                            "assets": [
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/",
                                    "output": "/assets/",
                                    "ignore": [
                                        "base/*",
                                        "css/*",
                                        "js/*"
                                    ]
                                },
                                {
                                    "glob": "**/*",
                                    "input": "src/assets/base",
                                    "output": "/"
                                }
                            ],
                            "fileReplacements": [
                                {
                                    "replace": "src/environments/environment.ts",
                                    "with": "src/environments/environment.prod.ts"
                                }
                            ],
                            "optimization": true,
                            "outputHashing": "all",
                            "sourceMap": false,
                            "extractCss": true,
                            "namedChunks": false,
                            "extractLicenses": true,
                            "vendorChunk": false,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ],
                            "outputPath": "dist/browser/",
                            "baseHref": "/",
                            "i18nFile": "src/locale/messages.en.xlf",
                            "i18nFormat": "xlf",
                            "i18nLocale": "en",
                            "i18nMissingTranslation": "error"
                        },
                        "production-fr": {
                            "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,
                            "budgets": [
                                {
                                    "type": "initial",
                                    "maximumWarning": "2mb",
                                    "maximumError": "5mb"
                                }
                            ],
                            "outputPath": "dist/browser/fr/",
                            "baseHref": "/fr/",
                            "i18nFile": "src/locale/messages.fr.xlf",
                            "i18nFormat": "xlf",
                            "i18nLocale": "fr",
                            "i18nMissingTranslation": "error"
                        }
                    }
                },
                "serve": {
                    "builder": "@angular-devkit/build-angular:dev-server",
                    "options": {
                        "browserTarget": "boilerplate:build:production-en"
                    },
                    "configurations": {
                        "fr": {
                            "browserTarget": "boilerplate:build:production-fr"
                        },
                        "en": {
                            "browserTarget": "boilerplate:build:production-en"
                        }
                    }
                },
                "extract-i18n": {
                    "builder": "@angular-devkit/build-angular:extract-i18n",
                    "options": {
                        "browserTarget": "boilerplate:build"
                    }
                },
                "test": {
                    "builder": "@angular-devkit/build-angular:karma",
                    "options": {
                        "main": "src/test.ts",
                        "karmaConfig": "./karma.conf.js",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "tsconfig.spec.json",
                        "scripts": [],
                        "styles": [
                            "src/styles.scss"
                        ],
                        "assets": [
                            "src/assets",
                            "src/favicon.ico"
                        ]
                    }
                },
                "lint": {
                    "builder": "@angular-devkit/build-angular:tslint",
                    "options": {
                        "tsConfig": [
                            "tsconfig.app.json",
                            "tsconfig.spec.json"
                        ],
                        "exclude": [
                            "**/node_modules/**"
                        ]
                    }
                }
            }
        },
        "boilerplate-e2e": {
            "root": "e2e",
            "sourceRoot": "e2e",
            "projectType": "application",
            "architect": {
                "e2e": {
                    "builder": "@angular-devkit/build-angular:protractor",
                    "options": {
                        "protractorConfig": "./protractor.conf.js",
                        "devServerTarget": "boilerplate:serve"
                    }
                },
                "lint": {
                    "builder": "@angular-devkit/build-angular:tslint",
                    "options": {
                        "tsConfig": [
                            "e2e/tsconfig.e2e.json"
                        ],
                        "exclude": [
                            "**/node_modules/**"
                        ]
                    }
                }
            }
        }
    },
    "defaultProject": "boilerplate",
    "schematics": {
        "@schematics/angular:component": {
            "prefix": "app",
            "styleext": "scss"
        },
        "@schematics/angular:directive": {
            "prefix": "app"
        }
    }
}

和package.json的一部分:

"scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "lint": "ng lint boilerplate",

        "build:prod:en": "ng build --configuration=production-en",
        "build:prod:fr": "ng build --configuration=production-fr",
        "build:server:prod:en": "ng run boilerplate:server:production-en",
        "build:server:prod:fr": "ng run boilerplate:server:production-fr",
        "build:library": "ng build ngx-example-library",
        "build:client-and-server-bundles": "npm run build:prod:en && npm run build:prod:fr && npm run build:server:prod:en && npm run build:server:prod:fr",
        "webpack:server": "webpack --config webpack.server.config.js --progress --colors",
        "build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
        "serve:ssr": "node dist/server"
    },

如前所述,我想使用i18n成功运行Angular应用程序。但是错误消息是:

  

[错误]错误:项目目标不存在。      在WorkspaceNodeModulesArchitectHost.getBuilderNameForTarget(/ Users / user $      在RunCommand.runSingleTarget(/ Users / user / Desktop / Boilerplate / boilerpla $      在RunCommand.runArchitectTarget(/ Users / user / Desktop / Boilerplate / boiler $      在RunCommand.run(/ Users / user / Desktop / Boilerplate / boilerplate / node_modu $      在RunCommand.validateAndRun(/ Users / user / Desktop / Boilerplate / boilerplat $      在process._tickCallback(内部/进程/next_tick.js:43:7)      在Function.Module.runMain(内部/模块/cjs/loader.js:778:11)      在启动时(internal / bootstrap / node.js:300:19)      在bootstrapNodeJSCore(internal / bootstrap / node.js:826:3)

如果有人已经遇到相同的问题并且可以向我解释我做错了什么?谢谢

4 个答案:

答案 0 :(得分:5)

我发现了这个。问题是:

1)您缺少architect文件中的angular.json配置。

2)您的browserTarget出了问题。目标必须指向项目,而不是Angular。

enter image description here

1)请将这些行添加到项目名称angular.json file => project => {{your project name}} => achitect: (remember to replace“ {{项目名称}}”`)

        "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/"{{project name}}"/server",
            "main": "server.ts",
            "tsConfig": "tsconfig.server.json"
          },
          "configurations": {
            "production": {
              "outputHashing": "media",
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "sourceMap": false,
              "optimization": true
            }
          }
        },
        "serve-ssr": {
          "builder": "@nguniversal/builders:ssr-dev-server",
          "options": {
            "browserTarget": ""{{project name}}":build",
            "serverTarget": ""{{project name}}":server"
          },
          "configurations": {
            "production": {
              "browserTarget": ""{{project name}}":build:production",
              "serverTarget": ""{{project name}}":server:production"
            }
          }
        },
        "prerender": {
          "builder": "@nguniversal/builders:prerender",
          "options": {
            "browserTarget": ""{{project name}}":build:production",
            "serverTarget": ""{{project name}}":server:production",
            "routes": [
              "/"
            ]
          },
          "configurations": {
            "production": {}
          }
        }

2)确保@nguniversal/express-engine中有@nguniversal/builderspackage.jsonnpm i @nguniversal/express-engine @nguniversal/builders -D

答案 1 :(得分:0)

我遇到了同样的问题,我的解决方法是添加“服务器”信息。
因为这行

“ build:client-and-server-bundles”:“ ng build --prod && ng运行ngkol:server:production”,

这是试图运行ng run ngkol:server:production <<<< / p>

"projects": {
    "ngkol": {
        "projectType":  "application",
        "schematics":   {},
        "root":         "src",
        "sourceRoot":   "src",
        "prefix":       "app",
        "architect": {

            //2019-08-23 here goes your other info
            // build, serve,extract-i18n, options, lint, e2e, 

            "server": {
                "builder": "@angular-devkit/build-angular:server",
                "options": {
                    "outputPath": "dist/server",
                    "main": "src/main.server.ts",
                    "tsConfig": "src/tsconfig.server.json"
                },
                "configurations": {
                    "production": {
                        "fileReplacements": [{
                            "replace": "src/environments/environment.ts",
                            "with": "src/environments/environment.prod.ts"
                        }]
                    }
                }
            }

我的应用在这里运行https://www.supercolegas.com/soon

答案 2 :(得分:0)

您可以尝试以下操作:

“服务器”:{                 “ builder”:“ @ angular-devkit / build-angular:server”,                 “选项”:{                     “ outputPath”:“ dist / server”,                     “ main”:“ src / main.server.ts”,                     “ tsConfig”:“ src / tsconfig.server.json” --------尝试用tsconfig.app.json替换tsconfig.server.json -------------                 },                 “配置”:{                     “生产”: {                         “ fileReplacements”:[{                             “ replace”:“ src / environments / environment.ts”,                             “ with”:“ src / environments / environment.prod.ts”                         }]                     }                 }             }

答案 3 :(得分:0)

我收到此错误是因为我的项目名称与我的工作目录名称略有不同。 因此,当我按照这个答案的建议写下“项目名称”时,我写的是我的项目目录的名称,而不是项目本身的名称。

项目名称可以在angular.json对象的projects文件中确认。