Home > Blockchain >  How to use jQuery libraries on a project with npm?
How to use jQuery libraries on a project with npm?

Time:04-02

I'm trying jQuery for the first time, I know this library is becoming more and more unused, but I need it to work with it on a project.

So I initialized a jQuery project with npm install jquery, so node modules and package-lock.json were created. I wonder how should I target library files on the index.html file, I mean maybe it should be something like this <script src="./node_modules/jquery/dist/jquery.min.js"></script>. Also how should I use libraries on .js files.

I don't know if I should point file by file or how should I use library files. Maybe someone could guide me with examples or docs on how to start using a jQuery libraries on a project.

CodePudding user response:

In your relevant javascript files (where you need to use jQuery) you should be doing

import $ from "jquery";

at the top. Also install and configure babel for compiling as it will support ES6/ES2015 modules even if browsers do not yet support.

CodePudding user response:

First you need to install jquery with npm.

Type this into terminal:

npm install jquery

I believe you have already completed that step successfully.

A window with a document is necessary for jQuery to operate in Node. Because such a window does not exist natively in Node, it can be faked using tools such as jsdom. This might be beneficial for testing.

const { JSDOM } = require( "jsdom" );
const { window } = new JSDOM( "" );
const $ = require( "jquery" )( window );

Please visit the jQuery documentation for details on how to get started and how to use jQuery. And also you can check the jQuery repo for source files and problems.These documentation will guide you.

Hope this may help you. Thank you!

  • Related