6

I'm new to Npm packages (coming from Ruby) and trying to load a jQuery plugin that's not necessarily on NPM. I'm building a Chrome extension. The code below is being used in the content.js script that's being injected in to the browser. So components like formatting and date pickers are desirable.

At the top of my app file, i have the following:

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

All works fine, but now I want to add a plugin, and I'm not quite sure where to put it and how to get it accessible to the app. I've tried just loading it like:

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

That doesn't work becausae it can't find $ in the script. So then I tried:

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

That didn't work either.

I clearly have no idea what I'm doing, but hoping this is easier than it appears.

Thanks!

2
  • 1
    Are you developing client-side code or server-side code? If you're developing server-side code, what are you trying to do with jQuery and a jQuery date-picker plugin? You probably need to back up and explain what you're actually trying to accomplish at a higher level. Commented Sep 22, 2014 at 15:25
  • Client-side. It's the content.js script for a chrome extension. Commented Sep 22, 2014 at 15:48

1 Answer 1

10

I had the same issue and did this:

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

Making $ and jQuery global satisfies plugins that I simply 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. In case you are interested, I use browserify to use node style modules client side.

Sign up to request clarification or add additional context in comments.

1 Comment

window.$ = window.jQuery = require('jquery'); if you want to do it in a single line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.