角度SSR - ReferenceError:未定义元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了角度SSR - ReferenceError:未定义元素相关的知识,希望对你有一定的参考价值。

我正在开发一个有角度的7项目。我使用服务器端渲染来允许Facebook爬虫正常工作。到目前为止,我已经开发了一些组件并且还没有遇到任何麻烦。但是现在我遇到了问题,因为我正在使用fullcalendar处理组件...

使用ng s为应用程序提供服务没有任何问题。但是当我尝试使用服务器端渲染npm run build:ssr && npm run serve:ssr构建应用程序时,我现在收到以下错误:npm ERR! code ELIFECYCLE

直到现在我做了一个npm installnpm audit fix,但这没有帮助。我认为这个问题来自于我的app.module.tsapp.server.module.ts中的错误配置,但我无法弄明白。

这里是错误消息的内容:

C:myAppdistserver.js:151900
    var matchesMethod = Element.prototype.matches ||
                        ^

ReferenceError: Element is not defined
    at C:myAppdistserver.js:151900:25
    at C:myAppdistserver.js:151801:13
    at Object.<anonymous> (C:myAppdistserver.js:151803:2)
    at __webpack_require__ (C:myAppdistserver.js:20:30)
    at Module.<anonymous> (C:myAppdistserver.js:151309:76)
    at __webpack_require__ (C:myAppdistserver.js:20:30)
    at Object.@fullcalendar/angular (C:myAppdistserver.js:134654:18)
    at __webpack_require__ (C:myAppdistserver.js:131212:30)
    at Module../node_modules/@fullcalendar/angular/fullcalendar-angular.ngfactory.js (C:myAppdistserver.js:131336:79)
    at __webpack_require__ (C:myAppdistserver.js:131212:30)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! silver-home@0.0.0 serve:ssr: `node dist/server`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the silver-home@0.0.0 serve:ssr 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!     C:UsersmyUserAppDataRoaming
pm-cache\_logs2019-04-28T20_11_07_675Z-debug.log

npm debug.log的内容:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'serve:ssr' ]
2 info using npm@6.4.1
3 info using node@v10.15.3
4 verbose run-script [ 'preserve:ssr', 'serve:ssr', 'postserve:ssr' ]
5 info lifecycle myApp@0.0.0~preserve:ssr: myApp@0.0.0
6 info lifecycle myApp@0.0.0~serve:ssr: myApp@0.0.0
7 verbose lifecycle myApp@0.0.0~serve:ssr: unsafe-perm in lifecycle true
8 verbose lifecycle myApp@0.0.0~serve:ssr: PATH: C:Program Files
odejs
ode_modules
pm
ode_modules
pm-lifecycle
ode-gyp-bin;C:myApp
ode_modules.bin;C:ProgramDataOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)QuickTimeQTSystem;C:xamppphp;C:ProgramDataComposerSetupin;C:WINDOWSSystem32OpenSSH;C:Program FilesGitcmd;C:Program Files (x86)Bitvise SSH Client;C:Program Files (x86)AOMEI Backupper;C:Program Files
odejs;C:Program Files (x86)GNU Tools Arm Embedded7 2018-q2-updatein;C:UsersmyUserAppDataLocalMicrosoftWindowsApps;C:UsersmyUserAppDataRoamingComposervendorin;C:Program Files (x86)GnuWin32in;C:mingwin;C:UsersUchenduAppDataRoaming
pm;C:UsersmyUserDesktopEtudesMath specprogrammationin;C:Program FilesJavajdk1.8.0_144in;
9 verbose lifecycle myApp@0.0.0~serve:ssr: CWD: C:myApp
10 silly lifecycle myApp@0.0.0~serve:ssr: Args: [ '/d /s /c', 'node dist/server' ]
11 silly lifecycle myApp@0.0.0~serve:ssr: Returned: code: 1  signal: null
12 info lifecycle myApp@0.0.0~serve:ssr: Failed to exec serve:ssr script
13 verbose stack Error: myApp@0.0.0 serve:ssr: `node dist/server`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:Program Files
odejs
ode_modules
pm
ode_modules
pm-lifecycleindex.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:189:13)
13 verbose stack     at ChildProcess.<anonymous> (C:Program Files
odejs
ode_modules
pm
ode_modules
pm-lifecyclelibspawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:189:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid myApp@0.0.0
15 verbose cwd C:myApp
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "serve:ssr"
18 verbose node v10.15.3
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error myApp@0.0.0 serve:ssr: `node dist/server`
22 error Exit status 1
23 error Failed at the myApp@0.0.0 serve:ssr script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

有什么建议我可以解决这个问题吗?

答案

问题来自第三方库。

https://github.com/fullcalendar/fullcalendar-angular

ReferenceError: Element is not defined
    at C:myAppdistserver.js:151900:25
    at C:myAppdistserver.js:151801:13
    at Object.<anonymous> (C:myAppdistserver.js:151803:2)
    at __webpack_require__ (C:myAppdistserver.js:20:30)
    at Module.<anonymous> (C:myAppdistserver.js:151309:76)
    at __webpack_require__ (C:myAppdistserver.js:20:30)
    at Object.@fullcalendar/angular (C:myAppdistserver.js:134654:18)
    at __webpack_require__ (C:myAppdistserver.js:131212:30)
    at Module../node_modules/@fullcalendar/angular/fullcalendar-angular.ngfactory.js (C:myAppdistserver.js:131336:79)
    at __webpack_require__ (C:myAppdistserver.js:131212:30)

看起来它没有导入日历组件的组件工厂,这让我觉得你缺少一些节点依赖。

确认您已安装@fullcalendar/core @fullcalendar/daygrid软件包。

该库正在使用另一个javascript库(“核心”包),它不是为服务器端呈现而设计的。虽然您可以编译项目。如果“核心”库中有任何无法在NodeJS上运行的内容,那么它可能无法编译或无法运行。

以上是关于角度SSR - ReferenceError:未定义元素的主要内容,如果未能解决你的问题,请参考以下文章

未捕获的 ReferenceError:未定义角度 - Mean.IO

使用打字稿时出现“未捕获的 ReferenceError:未定义角度”

未捕获的 ReferenceError:未定义角度 - AngularJS 不工作

角度图集成角度6

Angular Universal:未定义导航器

Angular 中 angular.copy 的替代方法是啥