我正在尝试将服务器端渲染集成到我的Angular 5网站中。我一直在使用本指南:https://www.genuitec.com/angular-5-firebase-angular-universal/该指南非常密切地反映了Angular自己的指南。我的实现只在绝对必要的地方有所不同。
作为实施的一部分,它建议将以下脚本添加到 package.json 文件中:
"build:universal": "npm run build:client-and-server-bundles && npm run webpack:server",
"serve:universal": "node dist/server.js",
"build:client-and-server-bundles": "ng build --prod && ng build --prod --app 1 --output-hashing=false",
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"
但是,当我运行npm run build:universal
时,我得到了以下结果:
> commonwealthlinen@0.0.0 build:universal /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1
> npm run build:client-and-server-bundles && npm run webpack:server
> commonwealthlinen@0.0.0 build:client-and-server-bundles /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1
> ng build --prod && ng build --prod --app 1 --output-hashing=false
Date: 2018-02-22T14:07:04.059Z
Hash: cf295e0d6c63da436ecd
Time: 50320ms
chunk {0} main.ef135b8d11eba44c66eb.bundle.js (main) 892 kB [initial] [rendered]
chunk {1} polyfills.ec6ea079491a34c09d8d.bundle.js (polyfills) 59.1 kB [initial] [rendered]
chunk {2} styles.d41d8cd98f00b204e980.bundle.css (styles) 0 bytes [initial] [rendered]
chunk {3} inline.19a4c06712152fdead2c.bundle.js (inline) 1.45 kB [entry] [rendered]
Date: 2018-02-22T14:07:19.302Z
Hash: de693eccb257cfc2c294
Time: 12671ms
chunk {0} main.bundle.js (main) 188 kB [entry] [rendered]
chunk {1} styles.bundle.css (styles) 0 bytes [entry] [rendered]
> commonwealthlinen@0.0.0 webpack:server /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1
> webpack --config webpack.server.config.js --progress --colors
Hash: 3ac91d65aa5c64d8b525
Version: webpack 3.11.0
Time: 28507ms
Asset Size Chunks Chunk Names
server.js 6.51 MB 0 [emitted] [big] server
[124] ./src lazy 160 bytes {0} [built]
[229] ./server.ts 1.79 kB {0} [built]
[296] ./src 160 bytes {0} [built]
[302] (webpack)/buildin/module.js 517 bytes {0} [built]
[315] ./dist/server/main.bundle.js 188 kB {0} [built]
[405] (webpack)/buildin/harmony-module.js 596 bytes {0} [built]
+ 440 hidden modules
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts(249,9)
TS2420: Class 'ZoneAwarePromise<R>' incorrectly implements interface 'Promise<R>'.
Property '[Symbol.toStringTag]' is missing in type 'ZoneAwarePromise<R>'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts(255,7)
TS2322: Type 'ZoneAwarePromise<any>' is not assignable to type 'Promise<R>'.
Property '[Symbol.toStringTag]' is missing in type 'ZoneAwarePromise<any>'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts(259,7)
TS2322: Type 'ZoneAwarePromise<any>' is not assignable to type 'Promise<U>'.
Property '[Symbol.toStringTag]' is missing in type 'ZoneAwarePromise<any>'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts(310,7)
TS2322: Type 'ZoneAwarePromise<R>' is not assignable to type 'Promise<R>'.
Property '[Symbol.toStringTag]' is missing in type 'ZoneAwarePromise<R>'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/common/promise.ts(333,13)
TS2322: Type 'ZoneAwarePromise<TResult1 | TResult2>' is not assignable to type 'Promise<TResult1 | TResult2>'.
Property '[Symbol.toStringTag]' is missing in type 'ZoneAwarePromise<TResult1 | TResult2>'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts(136,11)
TS2451: Cannot redeclare block-scoped variable 'Zone'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts(493,6)
TS2300: Duplicate identifier 'HasTaskState'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts(500,6)
TS2300: Duplicate identifier 'TaskType'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts(505,6)
TS2300: Duplicate identifier 'TaskState'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/zone.js/lib/zone.ts(628,7)
TS2451: Cannot redeclare block-scoped variable 'Zone'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/subscribe.test.ts(17,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/subscribe.test.ts(18,24)
TS2307: Cannot find module 'sinon'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts(82,7)
TS2304: Cannot find name 'before'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/errors.test.ts(86,7)
TS2304: Cannot find name 'after'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/deepCopy.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/deepCopy.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/base64.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/firebase/node_modules/@firebase/util/test/base64.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/subscribe.test.ts(17,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/subscribe.test.ts(18,24)
TS2307: Cannot find module 'sinon'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts(82,7)
TS2304: Cannot find name 'before'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/errors.test.ts(86,7)
TS2304: Cannot find name 'after'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/deepCopy.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/deepCopy.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/base64.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/util/test/base64.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/polyfill/src/polyfills/promise.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/polyfill/src/polyfills/promise.ts(17,7)
TS2451: Cannot redeclare block-scoped variable '__global'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/subscribe.test.ts(17,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/subscribe.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/subscribe.test.ts(18,24)
TS2307: Cannot find module 'sinon'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts(82,7)
TS2304: Cannot find name 'before'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/errors.test.ts(86,7)
TS2304: Cannot find name 'after'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/deepCopy.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/deepCopy.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/base64.test.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/messaging/node_modules/@firebase/util/test/base64.test.ts(16,24)
TS2307: Cannot find module 'chai'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/firestore/index.node.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/firestore/index.node.ts(19,27)
TS2307: Cannot find module './src/api/database'.
ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/firestore/index.node.ts
[tsl] ERROR in /Users/kwlester/Documents/JurassicRevenge/commonwealthlinen1/functions/node_modules/@firebase/firestore/index.node.ts(20,38)
TS2307: Cannot find module './src/platform/config'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! commonwealthlinen@0.0.0 webpack:server: `webpack --config webpack.server.config.js --progress --colors`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the commonwealthlinen@0.0.0 webpack:server script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/kwlester/.npm/_logs/2018-02-22T14_07_48_823Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! commonwealthlinen@0.0.0 build:universal: `npm run build:client-and-server-bundles && npm run webpack:server`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the commonwealthlinen@0.0.0 build:universal script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/kwlester/.npm/_logs/2018-02-22T14_07_48_858Z-debug.log
据我所知,初始化firebase功能导致了这个问题。我已经能够在一个vanilla 5安装中实现服务器端渲染,但是,只要我使用firebase init函数,我就会开始抛出错误。
如果我删除功能目录中的 node_modules 目录,则错误消失。为什么抛出这些错误,我怎样才能满足它们呢?
修改
这是我正在使用的 tsconfig.server.json 。这是Angular网站的直接副本:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "commonjs",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
],
"angularCompilerOptions": {
"entryModule": "app/app.server.module#AppServerModule"
}
}