JQuery plugin when using npm modules

I'm new to Npm packages (from Ruby) and trying to download a jQuery plugin that doesn't have to be on NPM. I am creating a Chrome extension. The code below is used in a content.js

script that is injected into the browser. Therefore, components such as formatting and date pickers are desirable.

At the top of my application file, I have the following:

var React = require("react");
var $ = require("jquery");
var moment = require("moment");

      

Everything works fine, but now I want to add a plugin and I'm not quite sure where to put it and how it can be obtained in the application. I tried to just load it like:

var React = require("react");
var $ = require("jquery");
require("./jquery-datepicker");
var moment = require("moment");

      

It doesn't work because it can't find it $

in the script. So I tried:

require("./jquery-datepicker")($);

      

It didn't work either.

I obviously don't know what I am doing, but I hope this is easier than it sounds.

Thank!

+3


source to share


1 answer


I had the same problem:

window.jQuery = require('jquery');
window.$ = window.jQuery;

      

Creating $ and jQuery global satisfies the plugins I just use like this:



require('./jquery-datepicker');

      

In all other cases, I avoid globals, but in the case of jQuery, I think you are going against the grain if you don't. If you're interested, I am using a browser to use the client side of node style modules.

+6


source







All Articles