10

我们目前正在迁移一个混合的 Angular6 和 AngularJS 1.6 项目。在这个过程中,我们还想引入服务器端渲染(SSR)。

问题是,SSR 在节点环境中运行,因此运行 SSR/prerendering 会出现以下错误:

npm-run ts-node ./prerender.ts

[..]/node_modules/angular/index.js:2

**module.exports = angular;**

**ReferenceError: angular is not defined**

   at Object.<anonymous> ([..]/node_modules/angular/index.js:2:18)

    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

    at Object.angular ([..]/dist/server/webpack:/external "angular":1:1)

    at _webpack_require_ 
([..]/dist/server/webpack:/webpack/bootstrap:19:1)

import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import {renderModuleFactory} from '@angular/platform-server';
import {writeFileSync} from 'fs';

const globalAny:any = global;
const Window = require('window');
let window = new Window();
globalAny.window = window;
globalAny.document = window.document;

const {AppServerModuleNgFactory} = require('./dist/server/main');

renderModuleFactory(AppServerModuleNgFactory, {
  document: '<app-root></app-root>',
  url: '/'
})
  .then(html => {
    console.log('Pre-rendering successful, saving prerender.html');
    writeFileSync('./prerender.html', html);
  })
  .catch(error => {
    console.error('Error occurred:', error);
  });

该项目基于universal-starter repo:https ://github.com/angular/universal-starter

有什么建议吗?

4

0 回答 0