GrapesJS Web Builder Framework
GrapesJS is a free and open-source Web Builder Framework designed to facilitate the creation of HTML templates, websites, newsletters, and mobile applications. It's primarily intended for integration into Content Management Systems (CMS) to provide a powerful visual editing experience. The current stable version is `0.22.15`, with the project demonstrating a positive and sustainable release cadence, frequently publishing patch releases. Key differentiators include its framework-centric approach, offering core UI components like a Block Manager, Style Manager, Layer Manager, Code Viewer, and Asset Manager. It focuses on generating clean, standards-based HTML and CSS output, unlike some other tools that might rely on specific frontend frameworks.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'init')
cause The `grapesjs` object was not correctly loaded or imported before `init()` was called, often due to incorrect script order or an invalid import path.fixEnsure `import grapesjs from 'grapesjs';` is at the top of your module, or for global scripts, that `<script src="path/to/grapes.min.js"></script>` is loaded before attempting to call `grapesjs.init()`. -
GrapesJS is not defined
cause This error typically occurs in a browser environment when the GrapesJS script has not been loaded or has failed to execute, meaning the global `grapesjs` object is unavailable.fixVerify the CDN link or local script path for `grapes.min.js` is correct and accessible. Ensure the script tag is placed before any inline script that attempts to use `grapesjs`. -
BUG: Cannot read properties of null (reading 'hasFocus') in Canvas module
cause Reported in recent GrapesJS versions (e.g., v0.22.11 or v0.22.14) in certain browser environments (Safari, Chrome) related to the Canvas module.fixThis is an active bug. Check the GrapesJS GitHub issues for potential fixes, workarounds, or official patch releases. Temporarily, you might need to try slightly older patch versions or implement custom logic to guard against `null` references where `hasFocus` is called.
Warnings
- breaking The global `GrapesJS` variable and main `grapesjs` export changed casing from `GrapesJS` (capital 'G') to `grapesjs` (lowercase 'g') around v0.16.x. Projects upgrading from much older versions may need to update their imports/global references.
- breaking From v0.20.x, the API for registering custom components via `typeModelOrView.extend` became deprecated. Users should switch to the modern `Components.addType('component-id', { model: {...} })` syntax.
- gotcha When initializing GrapesJS, ensure that the core CSS file (`grapes.min.css`) is properly loaded. Without it, the editor UI will appear unstyled and broken, leading to a poor user experience.
- gotcha Issues with undo/redo functionality or unexpected behavior after calling `component.replaceWith()` during lifecycle events can occur if the UndoManager is enabled. This can lead to editor crashes.
Install
-
npm install grapesjs -
yarn add grapesjs -
pnpm add grapesjs
Imports
- grapesjs
const grapesjs = require('grapesjs');import grapesjs from 'grapesjs'; import 'grapesjs/dist/css/grapes.min.css';
- Editor
import type { Editor } from 'grapesjs'; - GrapesJS (global)
var editor = Grapesjs.init(...); // incorrect casing
<script src="//unpkg.com/grapesjs@0.22.15"></script> <link rel="stylesheet" href="//unpkg.com/grapesjs@0.22.15/dist/css/grapes.min.css">
Quickstart
import grapesjs from 'grapesjs';
import 'grapesjs/dist/css/grapes.min.css';
// Ensure a DOM element exists for the editor
document.body.innerHTML = '<div id="gjs"></div>';
const editor = grapesjs.init({
container: '#gjs',
height: '700px',
width: 'auto',
fromElement: true, // Loads HTML from the container element
storageManager: {
type: 'local', // Use local storage
autosave: true, // Autosave content
autoload: true, // Autoload content on editor init
stepsBeforeSave: 1, // Number of changes before autosave
},
// Add some basic blocks
blockManager: {
blocks: [
{
id: 'section', // id is a unique and required property
label: '<b>Section</b>', // You can use HTML for the label
category: 'Basic',
content: '<section><h1>This is a section</h1></section>',
},
{
id: 'text',
label: 'Text',
category: 'Basic',
content: '<div data-gjs-type="text">Insert your text here</div>',
},
{
id: 'image',
label: 'Image',
category: 'Basic',
activate: true,
select: true,
content: { type: 'image' },
}
],
},
// Configure the panels (optional)
panels: {
defaults: [
{ id: 'commands', buttons: [{ id: 'undo', command: 'undo' }, { id: 'redo', command: 'redo' }] },
{ id: 'options', buttons: [{ id: 'sw-visibility', command: 'sw-visibility', context: 'sw-visibility' }] },
{ id: 'views', buttons: [{ id: 'open-blocks', command: 'open-blocks', active: true }] }
]
},
});
// Log the HTML and CSS when the editor changes
editor.on('update', () => {
console.log('HTML:', editor.getHtml());
console.log('CSS:', editor.getCss());
});
console.log('GrapesJS Editor initialized successfully.');