jQuery & AssetMapper #50733
-
I had previously followed this to install jQuery but I no longer use Webpack/Encore. I'm now using AssetMapper but I'm not sure how I can use jQuery in my app.js, I'm getting require is not defined and $ is not defined. I have already installed jQuery via importmap:require. I'd appreciate any help. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
maybe @weaverryan will help |
Beta Was this translation helpful? Give feedback.
-
I still get But if I remove jquery completely from AssetMapper, and add jQuery using the line below, I don't get
Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
I've managed to get it to work:
I've also had to do the same for bootstrap so I can use bootstrap vars in js. |
Beta Was this translation helpful? Give feedback.
-
Ah, I think I understand the situation: you want to use jquery AND have it become a global $ variable. Like Encore, when you import jquery, it doesn’t become a global variable (though Encore had the autoProvideJquery() method to fake / force this). so yes, if you want a global variable, your solution is correct: import it then make it global via window.$ 👍 But the hope is that, instead of relying on global variables, you import jquery wherever you need it. So in each file (or, I think even online script tags with type=“module” this would work, though I’d minimize use of those), where you are referencing $, do: import $ from ‘jquery’; Cheers! |
Beta Was this translation helpful? Give feedback.
Ah, I think I understand the situation: you want to use jquery AND have it become a global $ variable. Like Encore, when you import jquery, it doesn’t become a global variable (though Encore had the autoProvideJquery() method to fake / force this).
so yes, if you want a global variable, your solution is correct: import it then make it global via window.$ 👍
But the hope is that, instead of relying on global variables, you import jquery wherever you need it. So in each file (or, I think even online script tags with type=“module” this would work, though I’d minimize use of those), where you are referencing $, do:
import $ from ‘jquery’;
Cheers!