In my angular, I am loading jquery
from extranal and it works. But, after the build
, I am getting an error Uncaught ReferenceError: jQuery is not defined
. My build downloads the jquery
, mind you.
Question: How can I map the jquery
with my build?
index.html:
<script type="systemjs-importmap">
{
"imports": {
"single-spa": "https://cdn.jsdelivr.net/npm/[email protected]/lib/system/single-spa.min.js",
"jquery": "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"
}
}
</script>
<script>
System.import("single-spa");
System.import("jquery");
</script>
webpack config ( it works well locally )
module.exports = {
externals: {
jquery: ["jQuery"],
},
};
Note: After the build, if I run it in server, I get the error:
Uncaught ReferenceError: jQuery is not defined
at Object.xeH2 (main-es2015.3ee70c890346f45d7daa.js:2)
at l (runtime-es2015.cdfb0ddb511f65fdc0a0.js:1)
at Module.zUnb (main-es2015.3ee70c890346f45d7daa.js:2)
at l (runtime-es2015.cdfb0ddb511f65fdc0a0.js:1)
at Object.0 (main-es2015.3ee70c890346f45d7daa.js:2)
at l (runtime-es2015.cdfb0ddb511f65fdc0a0.js:1)
at t (runtime-es2015.cdfb0ddb511f65fdc0a0.js:1)
at Array.r [as push] (runtime-es2015.cdfb0ddb511f65fdc0a0.js:1)
at main-es2015.3ee70c890346f45d7daa.js:2
My server try to download the jquery
from an external source.
Explanation
When webpack externals are an object, the keys are the name you import in your code, and the values are the name of the module in the runtime environment.
The "runtime environment" used by the webpack bundle could either be global variables or the SystemJS module registry, depending on your webpack config's output.libraryTarget. The behavior of webpack externals changes for different libraryTargets.
If your libraryTarget is
"system"
(recommended for SystemJS loading),"umd"
, or"amd"
, then the bug here is that the name of the module in SystemJS module registry isjquery
, but your webpack config expects it to bejQuery
. SystemJS module names are case sensitive.If your libraryTarget is unspecified or
var
, then webpack will look for a global variable calledjQuery
. If you're using the systemjsamd.js
extra, then jQuery will not be created as a global variable. But if you are not using amd.js, then jQuery will be created as a global variable.Possible fixes
jquery
instead ofjQuery
. This will only work if the webpack libraryTarget issystem
,umd
, oramd
(as explained above).That webpack config is equivalent to providing externals as an array of strings:
var
.Further diagnosis
To diagnose the issues here, you can run the following in the browser console:
This will print how the jquery module was loaded by SystemJS. You should click on the logged value to expand it and see the keys. You should see jquery functions that you're familiar with it.