无法构建 angular 通用服务器,请帮助我
Cant build angular universal server, pls help me
当我尝试启动服务器时,遇到这个问题:
cmsfront@0.0.0 serve:ssr D:\PROGRAMIRANJE\CMSfrontbackup
node dist/CMSfront/server/main.js
D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764
抛出新错误("package.json does not exist at " + package_json_path);
^
错误:package.json 不存在于 D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\package.json
在对象../node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js.exports.find (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764:15)
在对象../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)
在对象../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)
在对象../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)
在对象../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)
在对象../node_modules/@firebase/firestore/dist/index.node.cjs.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:118582:12)
错误!代码生命周期
错误!错误号 1
错误! cmsfront@0.0.0 serve:ssr: node dist/CMSfront/server/main.js
错误!退出状态 1
错误!
错误!在 cmsfront@0.0.0 serve:ssr 脚本处失败。
错误!这可能不是 npm 的问题。上面可能还有其他日志记录输出。
npm 错误!此 运行 的完整日志可在以下位置找到:
错误! C:\Users\Lenovo Z50-70\AppData\Roaming\npm-cache_logs20-04-05T14_07_24_462Z-debug.log
我看到粗体 webpack_require 但是当我安装 nguniversal 时我没有得到 webpack 配置文件。
这是我的时间线:
- ng 添加@nguniversal/express-engine
- npm 运行 build:ssr
- npm 运行 serve:ssr - 来自顶部的错误。
请帮我解决这个问题。
编辑:
package.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"
}
在 angular.json server.options 中添加:
"externalDependencies": [
"grpc",
"@grpc/proto-loader",
"@angular/fire/firestore",
"firebase/firestore"
]
在 tsconfig.json > angularCompilerOptions
中添加了这个
"enableIvy": false
解决了我的问题,但我不知道我因假常春藤损失了多少。
当我尝试启动服务器时,遇到这个问题:
cmsfront@0.0.0 serve:ssr D:\PROGRAMIRANJE\CMSfrontbackup node dist/CMSfront/server/main.js
D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764 抛出新错误("package.json does not exist at " + package_json_path); ^
错误:package.json 不存在于 D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\package.json
在对象../node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js.exports.find (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:196764:15)
在对象../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)
在对象../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)
在对象../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)
在对象../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)
在对象../node_modules/@firebase/firestore/dist/index.node.cjs.js (D:\PROGRAMIRANJE\CMSfrontbackup\dist\CMSfront\server\main.js:118582:12)
错误!代码生命周期
错误!错误号 1
错误! cmsfront@0.0.0 serve:ssr: node dist/CMSfront/server/main.js
错误!退出状态 1
错误!
错误!在 cmsfront@0.0.0 serve:ssr 脚本处失败。
错误!这可能不是 npm 的问题。上面可能还有其他日志记录输出。
npm 错误!此 运行 的完整日志可在以下位置找到: 错误! C:\Users\Lenovo Z50-70\AppData\Roaming\npm-cache_logs20-04-05T14_07_24_462Z-debug.log
我看到粗体 webpack_require 但是当我安装 nguniversal 时我没有得到 webpack 配置文件。 这是我的时间线:
- ng 添加@nguniversal/express-engine
- npm 运行 build:ssr
- npm 运行 serve:ssr - 来自顶部的错误。
请帮我解决这个问题。
编辑: package.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"
}
在 angular.json server.options 中添加:
"externalDependencies": [
"grpc",
"@grpc/proto-loader",
"@angular/fire/firestore",
"firebase/firestore"
]
在 tsconfig.json > angularCompilerOptions
中添加了这个 "enableIvy": false
解决了我的问题,但我不知道我因假常春藤损失了多少。