工作区中未设置 Angular 12 通用配置“开发”

Tat*_*ova 6 node.js universal express angular

我在 Angular 11 上创建了新项目 - 客户端。然后将 ng 版本更新为 12 Angular。然后安装通用:ng add @nguniversal/express-engine

然后我尝试运行我的 Universal Angular,但它抛出错误:

npm run dev:ssr

client@0.0.0 dev:ssr ng 运行 client:serve-ssr


这是一个简单的服务器,用于在本地测试或调试 Angular 应用程序。它尚未经过安全问题审查。

不要将其用于生产!


工作区中未设置配置“开发”。npm 错误!代码 1 npm ERR!路径 E:\PRACTICE\MYPETPROJECTS\tanechka\client npm ERR!命令失败 npm ERR!命令 C:\WINDOWS\system32\cmd.exe /d /s /c ng run client:serve-ssr

npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!C:\Users\1\AppData\Local\npm-cache_logs\2021-06-08T14_35_07_681Z-debug.log

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "client": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/client/browser",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          },
          "defaultConfiguration": ""
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "client:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "client:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "client: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": "client:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "client:serve:production"
            }
          }
        },
        "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/client/server",
            "main": "server.ts",
            "tsConfig": "tsconfig.server.json",
            "optimization": false,
            "sourceMap": true,
            "extractLicenses": false
          },
          "configurations": {
            "production": {
              "outputHashing": "media",
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "sourceMap": false,
              "extractLicenses": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve-ssr": {
          "builder": "@nguniversal/builders:ssr-dev-server",
          "configurations": {
            "development": {
              "browserTarget": "client:build:development",
              "serverTarget": "client:server:development"
            },
            "production": {
              "browserTarget": "client:build:production",
              "serverTarget": "client:server:production"
            }
          },
          "defaultConfiguration": "development"
        },
        "prerender": {
          "builder": "@nguniversal/builders:prerender",
          "options": {
            "routes": [
              "/"
            ]
          },
          "configurations": {
            "production": {
              "browserTarget": "client:build:production",
              "serverTarget": "client:server:production"
            },
            "development": {
              "browserTarget": "client:build:development",
              "serverTarget": "client:server:development"
            }
          },
          "defaultConfiguration": "production"
        }
      }
    }
  },
  "defaultProject": "client"
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "client",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "dev:ssr": "ng run client:serve-ssr",
    "serve:ssr": "node dist/client/server/main.js",
    "build:ssr": "ng build && ng run client:server",
    "prerender": "ng run client:prerender"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~12.0.3",
    "@angular/common": "~12.0.3",
    "@angular/compiler": "~12.0.3",
    "@angular/core": "~12.0.3",
    "@angular/forms": "~12.0.3",
    "@angular/platform-browser": "~12.0.3",
    "@angular/platform-browser-dynamic": "~12.0.3",
    "@angular/platform-server": "~12.0.3",
    "@angular/router": "~12.0.3",
    "@nguniversal/express-engine": "^12.0.1",
    "express": "^4.15.2",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~12.0.3",
    "@angular/cli": "~12.0.3",
    "@angular/compiler-cli": "~12.0.3",
    "@nguniversal/builders": "^12.0.1",
    "@types/express": "^4.17.0",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.7.1",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.3.3",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.6.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.2.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试在服务的“配置”中添加“开发”:

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "client:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "client:build:production"
            },
            "development": {
              "browserTarget": "client:build:production"
            }
          }
        },
Run Code Online (Sandbox Code Playgroud)

但它没有用。

hsu*_*hou 31

修复 Angular 12 Universal 正在运行serve:ssr但显示Configuration 'development' is not set in the workspace

我的 Angular 项目已从 11 更新到 12。

由于某种原因我需要添加角度通用并遇到同样的问题。

尝试添加developmentarchitect.configurations生产复制的内容,但不起作用。

最后,我删除:developmentserve-ssr.configurations.development,效果很好。

angular.json片段中

"serve-ssr": {
          "builder": "@nguniversal/builders:ssr-dev-server",
          "configurations": {
            "development": {
              "browserTarget": "<YOUR_PROJECT_NAME>:build", <--- remove `:development` here
              "serverTarget": "<YOUR_PROJECT_NAME>:server"  <--- remove `:development` here
            },
            "production": {
              "browserTarget": "<YOUR_PROJECT_NAME>:production",
              "serverTarget": "<YOUR_PROJECT_NAME>:server:production"
            }
          },
          "defaultConfiguration": "development"
        },
Run Code Online (Sandbox Code Playgroud)


小智 5

这里是答案在 Angular 中,您可以在Angular.json文件中创建自己的工作区,例如开发、生产。请遵循以下步骤:-

  1. 转到应用程序根文件夹中的 angular.json 文件
  2. 在项目>“YOUR_APP_NAME”>架构师>serve-ssr>配置下添加此
“发展”: {
      "browserTarget": "YOUR_APP_NAME:build:development",
      “serverTarget”:“YOUR_APP_NAME:服务器:开发”
            }

节点:如果已经添加 3. 转到架构师 >服务器> 配置添加此内容

“发展”: {
      “优化”:正确,
      “源地图”:假,
      “提取许可证”:true
      }
  1. 转到架构师>服务>配置添加此
“发展”: {
       "browserTarget": "YOUR_APP_NAME:build:development"
        }
  1. 如果您构建应用程序,请转到架构师>构建>配置添加此内容
“发展”:{
              “优化”:正确,
              "outputHashing": "全部",
              “源地图”:假,
              “extractCSS”:正确,
              “namedChunks”:假,
              “提取许可证”:正确,
              “vendorChunk”:假,
              “buildOptimizer”:正确,
              “预算”:[
                {
                  “类型”:“初始”,
                  “最大警告”:“2mb”,
                  “最大错误”:“5mb”
                },
                {
                  “类型”:“任何组件样式”,
                  “最大警告”:“6kb”,
                  “最大错误”:“10kb”
                }
              ]
            }
  1. 转到架构师>预渲染>配置添加此
“发展”: {
              "browserTarget": "YOUR_APP_NAME:build:development",
              “serverTarget”:“YOUR_APP_NAME:服务器:开发”
            }

这个配置对我有用然后你可以运行# npm run serve:ssr


Tat*_*ova 1

没有任何帮助。因此,我删除了使用 Angular 12 的项目,并使用 Angular 11 创建了一个新项目,并添加了 Universal,运行 dev:ssr ,它就可以工作了!