JQuery set up after npm install

jquerynpm

i am running npm install jquery --save-dev from the command prompt to installing jquery for my project. but when i am using jquery from my javascript file it always returns the error:'$ is not defined'.
I am following the install instruction from https://jquery.com/download/.
but if i run $from chrome console it works fine.

I am wondering did i just missed some important setup from my package.json file?

Best Answer

This usually indicates that jQuery is not loaded and JavaScript does not recognize $. Are you trying to run this code locally, on your own machine? If so, you will need to add a script tag before your script.js and load jQuery from a CDN:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

Regarding the javascript file, you are asking about then you can add one of the below lines as per your framework syntax to create a reference to $ from already installed npm package.

import $ from "jquery";

Or

var $ = require("jquery");

You can also refer the official package docs https://www.npmjs.com/package/jquery

Hope this will help you!