Closed hunterInt closed 3 years ago
I ran into the same issue. There is code in the jquery file that does not init the global variables if it is loaded in scenarios like parcel. This is the code from jquery:
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get jQuery.
// For environments that do not have a `window` with a `document`
// (such as Node.js), expose a factory as module.exports.
// This accentuates the need for the creation of a real `window`.
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info.
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
With parcel, it runs through the then block where it should run through the else in your scenario.
There is a workaround. You can change the line factory( global, true )
in the jquery file to factory( global, false )
. I don't like this workaround but it is the only one I am aware of.
@mschaaf that worked! Thanks! I was just pasting in the JS into a <script>
tag instead of pulling from a URL, which also works, but is very messy.
Is there a more trivial fix to this? Been toying with Parcel for the past few hours, and this seems to be a stopper. Currently trying to make https://github.com/fancyapps/fancybox work.
@MindTooth I'm still looking for a solution on importing other types of dependencies globally (besides jQuery). No luck here. :p
any solutions here? im running the same issue ;(
$ yarn add bootstrap@3 jquery babel-polyfill
// index.js
import 'babel-polyfill'
import("jquery").then(async (jquery) => {
$ = window.$ = window.jQuery = jquery
await import('bootstrap')
$(document).ready(function() {
console.log('hello')
})
})
//or
const jquery = require('jquery')
//
$ = window.$ = window.jQuery = jquery;
require('bootstrap')
$(document).ready(() => {
console.log("hello");
});
@reducm That worked for me. Thanks!
Is there a way to make $
available but NOT require jQuery as a dependency in package.json
? I'm using Parcel for a WordPress plugin, and I've got things working, including using PHP enqueueing which allows me to specify 'jquery' as a dependency, and WP already loads jQuery, so I just want to make sure I'm not double-including jQuery with both WP and my JS.
===
Update:
I got it working by using https://www.npmjs.com/package/parcel-plugin-externals (from https://github.com/parcel-bundler/parcel/issues/144#issuecomment-544297197)
I can confirm that this still exists in 2.0.0-alpha.3.2
, specifically with jQuery.
Based on the solution presented by @reducm but I didn't want jQuery as a global and I had no need for it to be accesible at jQuery()
only $()
in which case you can use this:
import('jquery').then(async ($) => {
// your code that uses jQuery
});
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.
This issue is still happening on 2.6.2
🐛 bug report
I've spent almost a week trying to figure out why I'm having such problems with jquery being required. My only guess is that it has something to do with how Parcel is handling asset files. As you'll see below, I've tried including jQuery in my index.html file multiple ways, CDN and locally, and my
jQuery(document).ready(function($) {console.log("jQuery loaded");...
seems to load correctly, but the other files that require jQuery are not able to access it.🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
jQuery should be loaded.
😯 Current Behavior
💁 Possible Solution
🔦 Context
Other local files, seem to have loaded and been imported just fine, at this point I've stripped the entire app down the minimum; so the only files required are the ones needed to produce the error.
💻 Code Sample
index.html:
🌍 Your Environment
ExpressJS sever: