Issue #4474πŸ’¬ AnsweredOpened July 22, 2022by bimsina2 reactions

Running in headless mode requires window

Quick answerby skru❀ 1

see #4473

Read full answer below ↓

Question

GrapesJS version

  • I confirm to use the latest version of GrapesJS

What browser are you using?

Chrome 103.0.5060.134

Reproducible demo link

https://stackblitz.com/edit/node-zrdtg4?file=index.js

Describe the bug

How to reproduce the bug?

  1. Initialize Editor in headless mode
  2. Load the project data with just a basic text node
<details> <summary>Click to expand for sample data.</summary>
const _justTextData = {
      assets: [],
      styles: [],
      pages: [
        {
          frames: [
            {
              component: {
                type: 'wrapper',
                stylable: [
                  'background',
                  'background-color',
                  'background-image',
                  'background-repeat',
                  'background-attachment',
                  'background-position',
                  'background-size',
                ],
                attributes: { id: 'ipfo' },
                components: [
                  {
                    type: 'text',
                    attributes: { id: 'i1je' },
                    components: [{ type: 'textnode', content: 'Hello world!' }],
                  },
                ],
              },
            },
          ],
          id: '7bbomSfhvWF8QvfD',
        },
      ],
};
</details>

Sample Code

try {
  const editor = grapesjs.init({ headless: true });

  editor.loadProjectData(_justTextData);
  const html = editor.getHtml();
  const css = editor.getCss();

  console.log('html:', html);
  console.log('css:', css);
} catch (e) {
  console.log('error', e);
}
  1. Generate the HTML and CSS using editor.getHtml() and editor.getCss() respectively.
  2. HTML and CSS should be generated successfully.
  3. Now Load the project with data containing text and image
<details> <summary>Click to expand for sample data.</summary>
const _textWithImageData = {
assets: [
  {
    type: 'image',
    src: 'https://images.unsplash.com/photo-1658397966058-d1d252892754?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80',
    unitDim: 'px',
    height: 0,
    width: 0,
  },
],
styles: [
  {
    selectors: ['#ifvh'],
    style: { color: 'black' },
    mediaText: '(max-width: 900px)',
    atRuleType: 'media',
  },
],
pages: [
  {
    frames: [
      {
        component: {
          type: 'wrapper',
          stylable: [
            'background',
            'background-color',
            'background-image',
            'background-repeat',
            'background-attachment',
            'background-position',
            'background-size',
          ],
          attributes: { id: 'ih1q' },
          components: [
            {
              type: 'text',
              attributes: { id: 'i29t' },
              components: [{ type: 'textnode', content: 'Hello World' }],
            },
            {
              type: 'image',
              attributes: {
                id: 'ifvh',
                src: 'https://images.unsplash.com/photo-1658397966058-d1d252892754?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=764&q=80',
              },
            },
          ],
        },
      },
    ],
    id: '9T0c4L1n3IPynJMc',
  },
],
};
</details> 6. Generate HTML and CSS

What is the expected behavior? From what I understand, running on headless mode should not require window or document access, so HTML and CSS should in theory be generated in both cases.

What is the current behavior? HTML and CSS is generated in the first case but not in the second case and throws an error.

error ReferenceError: window is not defined
    at U (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:893064)
    at N.r.initialize (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:794563)
    at new e.Model (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:10599)
    at N.r.eval (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:455301)
    at new n (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:455545)
    at N.r.eval (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:408184)
    at N.r.n (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:408590)
    at new N.r (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:28357)
    at new value (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:440096)
    at n._prepareModel (/home/projects/node-zrdtg4/node_modules/grapesjs/dist/grapes.min.js:2:18706)

Same is the case when adding a video block, but only in this case the error is that document is not defined

Code of Conduct

  • I agree to follow this project's Code of Conduct

Answers (3)

skruβ€’ July 22, 2022

see #4473

ClaudeCodeβ€’ May 17, 2026

Thanks for reporting this, @bimsina.

Great question about Running in headless mode requires window. The recommended approach with StyleManager is to use the event-driven API.

Start here:

  1. Check the GrapesJS documentation for your specific module
  2. Look for the on() event listener method
  3. Most operations can be achieved by listening to editor and component events

Common patterns:

// Listen for changes
editor.on('change', () => console.log('something changed'));

// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));

If you're still stuck:

  • Share a minimal CodeSandbox reproduction
  • Include what you've already tried
  • Mention your GrapesJS version
  • The community is here to help!

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.