执行ng add @nguniversal/express-engine时提示Schema不合法
问题场景
执行以下命令将Angular项目改造为Angular Universal时触发Schema不合法错误:
ng add @nguniversal/express-engine --clientProject cookbook
控制台错误信息
gettingstarted@gettingstarted:~/WebstormProjects/cookbook$ ng add @nguniversal/express-engine --clientProject cookbook
ℹ Using package manager: npm
✔ Found compatible package version: @nguniversal/express-engine@14.2.0.
✔ Package information loaded.The package @nguniversal/express-engine@14.2.0 will be installed and executed.
Would you like to proceed? Yes
✔ Packages successfully installed.
Schematic input does not validate against the Schema: {"clientProject":"cookbook","project":"cookbook"}
Errors:Data path "" must NOT have additional properties(clientProject).
gettingstarted@gettingstarted:~/WebstormProjects/cookbook$
项目angular.json配置
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "cli": { "analytics": false }, "version": 1, "newProjectRoot": "projects", "projects": { "cookbook": { "projectType": "application", "schematics": { "@schematics/angular:application": { "strict": true } }, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/cookbook", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css" ], "scripts": [ "./node_modules/bootstrap/dist/js/bootstrap.min.js", "./node_modules/jquery/dist/jquery.js", "./node_modules/@popperjs/core/dist/umd/popper.min.js" ] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "500kb", "maximumError": "1mb" }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb" } ], "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "outputHashing": "all" }, "development": { "buildOptimizer": false, "optimization": false, "vendorChunk": true, "extractLicenses": false, "sourceMap": true, "namedChunks": true } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "browserTarget": "cookbook:build:production" }, "development": { "browserTarget": "cookbook:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "browserTarget": "cookbook: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.css" ], "scripts": [] } } } } } }
解决方案
Angular 14+版本的@nguniversal/express-engine schematic已废弃--clientProject参数,改用--project参数。执行以下命令即可完成安装:
ng add @nguniversal/express-engine --project cookbook
如果项目是单项目结构(angular.json中仅包含一个项目),可直接省略参数,CLI会自动识别默认项目:
ng add @nguniversal/express-engine
内容的提问来源于stack exchange,提问作者GettingStarted

