1

As soon as I add import draw2d from "draw2d" to my imports I get ReferenceError: jQuery is not defined.

I tried installing and adding jquery but still get same error...

import React, {Component} from "react";
import jQuery from "jquery";
import draw2d from "draw2d";
TedTrippin
  • 3,525
  • 5
  • 28
  • 46

1 Answers1

3

Create a file with any name, e.g. import-jquery.js

import-jquery.js

import jquery from "jquery";

window.$ = window.jQuery = jquery;

App.js

import "import-jquery";
import "jquery-ui-bundle"; // you also need this
import "jquery-ui-bundle/jquery-ui.css";
import draw2d from "draw2d";

EDIT:

And don't forget to...

npm install jquery
npm install jquery-ui-bundle
cuzox
  • 794
  • 7
  • 15
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • Cheers Zohab. Just needed to correct the first import and thought I'd add the npm installs in case it wasn't obvious. – TedTrippin Apr 09 '20 at 15:02
  • ;) there you go. I already accepted as the answer. What about upvoting my question?? – TedTrippin Apr 09 '20 at 16:59
  • But why did you install `import-jquery` package when you can simply import `import-jquery.js`? – Zohaib Ijaz Apr 09 '20 at 17:00
  • cus if I didn't install it then it wasn't there to be able to import it :) – TedTrippin Apr 13 '20 at 18:27
  • Any idea when I get `Cannot read property 'x' of undefined` when I call `new draw2d.Canvas("draw2dHolder");`? – TedTrippin Apr 13 '20 at 18:28
  • @TedTrippin it depends on your code, can't tell just reading your statement. However, I would suggest you to not to use Draw2d since it is not being maintained well. I worked on this for something but it has some bugs and no community support. I would recommend using `fabric` JS which is easy to learn, you can achieve more complex functionalities using fabric. – Zohaib Ijaz Apr 14 '20 at 04:52