【问题标题】:Cant build angular universal server, pls help me无法构建 Angular 通用服务器,请帮助我
【发布时间】:2020-07-17 12:24:25
【问题描述】:

当我尝试启动服务器时,遇到这个问题:

cmsfront@0.0.0 服务:ssr D:\PROGRAMIRANJE\CMSfrontbackup 节点 dist/CMSfront/server/main.js

D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764 throw new Error("package.json 不存在于" + package_json_path); ^

错误:D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\package.json 中不存在 package.json 在 Object../node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js.exports.find (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764:15) 在 Object../node_modules/grpc/src/grpc_extension.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:211850:12) 在 webpack_require (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:20:30) 在 Object../node_modules/grpc/src/client_interceptors.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:209576:12) 在 webpack_require (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:20:30) 在 Object../node_modules/grpc/src/client.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:208381:27) 在 webpack_require (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:20:30) 在 Object../node_modules/grpc/index.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:191024:14) 在 webpack_require (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:20:30) 在 Object../node_modules/@firebase/firestore/dist/index.node.cjs.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:118582:12)
npm 错误!代码生命周期 npm 错误!错误号 1 npm 错误! cmsfront@0.0.0 服务:ssr:node dist/CMSfront/server/main.js npm 错误!退出状态 1 npm 错误! npm 错误!在 cmsfront@0.0.0 serve:ssr 脚本中失败。 npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。

npm 错误!可以在以下位置找到此运行的完整日志: npm 错误! C:\Users\Lenovo Z50-70\AppData\Roaming\npm-cache_logs\2020-04-05T14_07_24_462Z-debug.log

我看到加粗的 webpack_require,但是当我安装 nguniversal 时,我没有得到 webpack 配置文件。 这是我的时间表:

  1. ng 添加@nguniversal/express-engine
  2. npm run build:ssr
  3. npm run serve:ssr - 顶部出错。

请帮我解决这个问题。

编辑: 包.json

{
  "name": "cmsfront",
  "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 CMSfront:serve-ssr",
    "serve:ssr": "node dist/CMSfront/server/main.js",
    "build:ssr": "ng build --prod && ng run CMSfront:server:production",
    "prerender": "ng run CMSfront:prerender"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.0",
    "@angular/cdk": "^9.2.0",
    "@angular/common": "~9.0.0",
    "@angular/compiler": "~9.0.0",
    "@angular/core": "~9.0.0",
    "@angular/fire": "^5.4.2",
    "@angular/forms": "~9.0.0",
    "@angular/material": "^9.2.0",
    "@angular/platform-browser": "~9.0.0",
    "@angular/platform-browser-dynamic": "~9.0.0",
    "@angular/platform-server": "~9.0.0",
    "@angular/router": "~9.0.0",
    "@nguniversal/express-engine": "^9.0.2",
    "express": "^4.15.2",
    "firebase": "^7.13.1",
    "hammerjs": "^2.0.8",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.1",
    "@angular/cli": "~9.0.1",
    "@angular/compiler-cli": "~9.0.0",
    "@angular/language-service": "~9.0.0",
    "@nguniversal/builders": "^9.0.2",
    "@types/express": "^4.17.0",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "CMSfront": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/CMSfront/browser",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss"
            ],
            "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,
              "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": "CMSfront:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "CMSfront:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "CMSfront: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": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "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": "CMSfront:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "CMSfront:serve:production"
            }
          }
        },
        "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/CMSfront/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": "CMSfront:build",
            "serverTarget": "CMSfront:server"
          },
          "configurations": {
            "production": {
              "browserTarget": "CMSfront:build:production",
              "serverTarget": "CMSfront:server:production"
            }
          }
        },
        "prerender": {
          "builder": "@nguniversal/builders:prerender",
          "options": {
            "browserTarget": "CMSfront:build:production",
            "serverTarget": "CMSfront:server:production",
            "routes": [
              "/"
            ]
          },
          "configurations": {
            "production": {}
          }
        }
      }
    }
  },
  "defaultProject": "CMSfront"
}

【问题讨论】:

  • 向我们展示 package.json 文件。
  • @MikiBelavista 添加。

标签: node.js angular express npm angular-universal


【解决方案1】:

在 angular.json server.options 中添加了这个:

"externalDependencies": [
              "grpc",
              "@grpc/proto-loader",
              "@angular/fire/firestore",
              "firebase/firestore"
            ]

在 tsconfig.json 中添加了这个 > angularCompilerOptions

 "enableIvy": false

解决了我的问题,但我不知道我因伪造 Ivy 而损失了多少。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多