DEV Community

Discussion on: Managing ASP.NET Core MVC front-end dependencies with npm and webpack (part 2)

Collapse
 
martajohnsson profile image
Marta Johnsson

Hi, Your guide was very helpful. But I wounder how I can use jQuery now.
I'm just testing to add some script:
<br> $(document).ready(function () {<br> console.log(&quot;ready!&quot;);<br> });<br>
On a contact page in script section but getting: Uncaught ReferenceError: $ is not defined

I know I should not put code like that but I'm working on a huge refactoring and there is a ton of such scripts everywhere.

Collapse
 
larswillemsens profile image
Lars Willemsens

Hey, thanks for your comment! You won't be able to use jQuery in a script section using this setup. You can use jQuery in a JavaScript file that will be picked up by webpack, however.
If you do need jQuery in an inline JavaScript section, then I'd recommend adding reference to a jQuery CDN as well.
Hope this helps your refactoring. Good luck!!

Collapse
 
martajohnsson profile image
Marta Johnsson

Thank You for Your answer.
I did find another solution. I can use expose-loader to make it work.
{
test: require.resolve("jquery"),
loader: "expose-loader",
options: {
exposes: ["$", "jQuery"],
},
},

Thread Thread
 
larswillemsens profile image
Lars Willemsens

Great! That looks like an ideal solution. Thanks for the addition!