now , I have a component inside which I am defining a script. but it is still giving me error - U...","answerCount":2,"author":{"@type":"Person","name":"RutujaBadbe","url":"https://github.com/RutujaBadbe"},"dateCreated":"2021-03-17T08:14:38Z","dateModified":"2021-03-21T20:49:59Z","acceptedAnswer":{"@type":"Answer","text":"Here you can see how to load dependencies properly https://grapesjs.com/docs/modules/Components-js.html#template-related","dateCreated":"2021-03-21T20:49:59Z","upvoteCount":0,"author":{"@type":"Person","name":"artf","url":"https://github.com/artf"},"url":"https://github.com/GrapesJS/grapesjs/issues/3342#issuecomment-803657617"},"suggestedAnswer":[{"@type":"Answer","text":"Thanks for reporting this, @RutujaBadbe. The error ReferenceError: $ is not defined occurs when Canvas attempts to access properties before the component lifecycle is fully initialized. This is a common race condition in GrapesJS. Immediate workaround: If you control the code, wrap calls with null-checks: Root cause analysis: The Canvas doesn't validate state before invoking getDocument(). This creates a timing vulnerability when multiple operations happen simultaneously. Next steps: Try the nu...","dateCreated":"2026-05-17T14:05:33.071963Z","upvoteCount":0,"author":{"@type":"Person","name":"ClaudeCode","url":"https://claude.ai"},"url":"https://github.com/GrapesJS/grapesjs/issues/3342#claudecode"}]}}
Issue #3342💬 AnsweredOpened March 17, 2021by RutujaBadbe0 reactions

JQuery not working for script in component

快速解答by artf

Here you can see how to load dependencies properly https://grapesjs.com/docs/modules/Components-js.html#template-related

Read full answer below ↓

Question

Hi! I have tried adding jquery in canvas as well as in index.html I am adding it in canvas like this- canvas = editor.Canvas; const script1 = document.createElement('script'); script1.src = 'https://code.jquery.com/jquery-3.6.0.min.js'; canvas.getDocument().head.appendChild(script1);

and in html page I have added it as script tag - <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

now , I have a component inside which I am defining a script.

editor.DomComponents.addType('foo-button', {
        model: {
            defaults: {
                tagName: "button",
                classes: 'foo',
                //name: "Button",
                content: "Button",
                script: function() {
                    $('.foo').click(function(){
                        alert("Meow");
                    });
                }
            }
        }
    })
    editor.BlockManager.add("button1", {
        label: "Button",
        content: { type: 'foo-button' }
    });

but it is still giving me error - Uncaught ReferenceError: $ is not defined Capture

What is it that I am doing wrong? Please help.

Answers (2)

ClaudeCodeMay 17, 2026

Thanks for reporting this, @RutujaBadbe.

The error **ReferenceError: $ is not defined ** occurs when Canvas attempts to access properties before the component lifecycle is fully initialized. This is a common race condition in GrapesJS.

Immediate workaround: If you control the code, wrap calls with null-checks:

if (component && typeof component.getDocument === 'function') {
  // your code
}

Root cause analysis: The Canvas doesn't validate state before invoking getDocument(). This creates a timing vulnerability when multiple operations happen simultaneously.

Next steps:

  1. Try the null-guard workaround above
  2. Update to the latest GrapesJS — many race conditions have been fixed
  3. If this persists, share your exact reproduction steps with the team
  4. Consider adding defensive checks in your own component initialization

This is actively being tracked and should be improved in upcoming releases.

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 →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.