5

I'm developing a node library that depends on jQuery and jqueryUI. I use browserify to make it accessible to a browser page.
So in my library code, there can be for example such kind of code:

var createMenu = function (target, options) {
  $target = $(target);
  $target.menu(options);
}

I require jQuery and jQueryUI like this:

var $ = jQuery = require("./node_modules/jquery");
require("./node_modules/jquery-ui-dist");

but when I call createMenu function in the browser, it logs TypeError: $target.menu is not a function.

JQueryUI doesn't seem to be properly loaded but I didn't find documentation on how to load it properly.

I also tried $.ui = require('./node_modules/jquery-ui/ui/widget.js') without any more luck.

If I do the following:

jQueryUI = require("./node_modules/jquery-ui")
console.log(jQueryUI)

it only logs an empty object.

Any idea on the proper way to log jQueryUI to use it in nodejs?

6
  • 1
    Why do you need a frontend library (jQuery UI) in a backend (NodeJS) module? Commented Apr 4, 2018 at 9:07
  • 1
    @Sirko Nodejs is a convenient context to develop a library. Even if I use this library only in frontend after "browserifying" it. With node I can easily manage dependencies, import modules... Commented Apr 4, 2018 at 9:11
  • @chateau JQuery is not a good choice as it depends on the global window object which simply does not exist server side where node runs. JQuery is for traversing the DOM and some simple animations. I don't expect this is what you are trying to do server side? Maybe you can give more context of what you are trying to achieve? Commented Apr 4, 2018 at 9:19
  • @Strech0 Yes I'm not using jQuery in the server side, I use it in the browser (after "browserifying" the library). But I'm developing many libraries with node and node is convenient for requiring other modules, and it is a good development context for me as I have prebuilt utilities that I use in other libraries. However the functions from the library will be used from the browser, and in the browser, I pass a jQuery object to them that gives the DOM context. Commented Apr 4, 2018 at 9:29
  • 1
    @HéloïseChauvel nope, I didn't find any proper way, I'm still loading it from the browser, that's the only way I'm able to make it work so far. Commented Oct 18, 2018 at 13:00

1 Answer 1

1

These steps should work:

Install these packages from npm

npm install jquery
npm install jquery-ui-dist

Import them in HTML file, like

<script src="/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/node_modules/jquery-ui-dist/jquery-ui.min.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.