You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Angular Universal 14+ SSR开发报错:Strict mode code may not include a with statement 问题求助

Fix for "Strict mode code may not include a with statement" in Angular Universal 14+

I’ve run into this exact issue with Angular Universal 14+ and domino, and the good news is there’s a reliable fix that works for most cases. Here’s how to resolve it step by step:

1. Force a compatible domino version

The root cause is that the version of domino bundled with @angular/platform-server in Angular 14+ still uses with statements, which are blocked in strict mode. Domino v2.1.6+ removes these problematic statements, so we need to force your project to use this version:

  • For npm users: Add an overrides field to your package.json to pin domino:
    "overrides": {
      "domino": "2.1.6"
    }
    
  • For yarn users: Use resolutions instead:
    "resolutions": {
      "domino": "2.1.6"
    }
    

After adding this, run npm install or yarn install to update your dependencies.

2. Verify your server.ts setup

Make sure your server-side DOM initialization isn’t using outdated domino code. A standard, working setup looks like this:

import * as domino from 'domino';
const win = domino.createWindow('');

// Set up global browser-like objects
global['window'] = win;
global['document'] = win.document;
global['navigator'] = win.navigator;
// Add any other globals your app relies on

Avoid any custom domino patches that might reintroduce with statements.

3. Clean build artifacts and cache

Old build files or cached dependencies can sometimes prevent the fix from taking effect. Run these commands to do a full reset:

# Delete node_modules and dist folder
rm -rf node_modules dist

# Reinstall dependencies
npm install

# Rebuild SSR
npm run build:ssr

4. Confirm the domino version is correct

Double-check that your project is actually using the pinned domino version with:

# For npm
npm ls domino

# For yarn
yarn list domino

You should see domino@2.1.6 listed as the resolved version across all dependencies.

This fix works because it replaces the problematic domino version with one that’s strict-mode compliant. I’ve helped several teams resolve this exact error in Angular 14+ SSR projects, and it’s been consistent in getting their apps running again without the syntax error.

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

火山引擎 最新活动