You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 16 SSR启动报错:TypeError: instanceof右侧非对象

Angular 16 SSR启动报错:TypeError: Right-hand side of 'instanceof' is not an object

问题分析

你的问题出在Angular 16升级后,原有server.ts的配置与新版本Universal不兼容,尤其是手动模拟浏览器全局对象的部分。Angular 16对Universal底层实现做了优化,不再需要手动挂载大量domino模拟的全局对象,旧的全局变量挂载方式会触发内部类型判断异常。

解决方案

1. 同步Universal依赖版本

确保所有@nguniversal/*相关依赖版本与Angular 16完全一致,执行命令更新:

npm install @nguniversal/express-engine@16 @nguniversal/builders@16 --save

2. 简化server.ts配置

替换原有server.ts为适配Angular 16的版本,移除不必要的全局对象手动挂载:

import { enableProdMode } from '@angular/core';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { existsSync } from 'fs';
const dotenv = require('dotenv');
dotenv.config();

import 'zone.js/dist/zone-node';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/project-name/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

function run(): void {
  const port = process.env.PORT || 8000;
  // Start up the Node server
  const server = app();
  server.get('/env', (req, res) => {
    res.json(process.env);
  })
  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

3. 按需处理额外依赖(可选)

如果项目不需要手动模拟WebSocket和XMLHttpRequest,直接移除这两行:

(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xhr2');

若确实需要这些功能,确保ws和xhr2版本与当前Node.js版本兼容。

4. 重新构建启动

执行命令重新构建项目并启动SSR:

npm run build:ssr
npm run serve:ssr

关键说明

Angular 16的Universal已经内置了更完善的服务器端DOM模拟,无需手动挂载DOMTokenList、Node、Text等对象,原有的手动挂载会破坏Angular内部的类型判断逻辑,从而触发instanceof相关错误。

内容的提问来源于stack exchange,提问作者Santosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:16:23