1

I'm using a jquery plugin for calendar http://kylestetz.github.io/CLNDR/ Currently I'm using it with CDN But I want to use it as a nodejs module what is the best way to do it ?

Edit: I'm using babel Transpiler and es6 syntax.

12
  • 1
    jQuery is mainly for DOM manipulation, why do you need it in node ? are you parsing HTML ? Commented Jun 28, 2016 at 9:33
  • because I'm using babel Commented Jun 28, 2016 at 9:36
  • How would using babel force you to need to parse HTML using jQuery? Commented Jun 28, 2016 at 19:20
  • @Sagar You don't make sense. Babel does not require jQuery plugins. Commented Jun 28, 2016 at 19:38
  • @Bergi But my project needs it. I'm updating my question to make it more clear. Commented Jun 29, 2016 at 4:59

1 Answer 1

1

If the plugin has not been written as an ES6 module you would need to do this yourself. Since jQuery plugins simply attach themselves to an existing jQuery object they don't really export anything of note. So you can simply wrap the plugin in a immediately invoked function and export that. Remember to either import jQuery in the plugin file or make it available globally.

import jQuery from 'jquery';

const calendar = (function ($) {
    ... your jquery plugin code
    ... can live here 
}(jQuery));

If you feel inclined to learn from this you could also fork the project and rewrite the plugin code as an ES6 class and make it available on npm for others.

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

1 Comment

If I write a jQuery plugin like in your example, should I put "jquery" in the peerDependencies section of my plugin's package.json? This way, if I add to the jQuery prototype, I will add to the same jQuery object the consumer of my plugin/library. Or is it enough to use jquery as a normal dependency (dependencies)?

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.