5

I', using browserify to transform node modules to browser ones for my express app.

This is the command to browserify

browserify -r jquery > ./public/js_app/jquery.js
browserify -r jquery-ui-browserify > ./public/js_app/jquery-ui.js

And this is to require jquery

var jQuery = require('jquery');

this code works fine

jQuery("#info-dialog");

but I don't know how to include jquery-ui-browserify. I have tried this but not works

var jQuery = require('jquery');
require('jquery-ui-browserify');
jQuery("#info-dialog").dialog({
  resizable: false,
  height: 100,
  modal: true
}); 

Thanks

1
  • 1
    Why don't you just require jQuery and jQuery UI in one file and then bundle that up? Also, you should assign some variable to require('jquery-ui-browserify'). For example: var jQueryUI = require('jquery-ui-browserify'); Commented Oct 7, 2014 at 7:30

1 Answer 1

7

Here's how i'd set this up

So, from the top:

  1. create project dir:
    • mkdir myProject && cd ./myProject to create directory
    • npm init will interactively create your package.json
  2. install dependencies
    • sudo npm install jquery jquery-ui-browserify --save
      • this installs both and --save adds them to your package.json
  3. Create app structure
    • mkdir app will create your main folder
    • cd app && touch index.html app.js
  4. Write your script in app.js, requiring and using all you wish:
    • first var $ = require('jquery'); require('jquery-ui-browserify');
    • below that, write script as normal
  5. Browserify that jank!
    • open terminal, if not still in /myProject/app/, cd into it
    • run browserify ./app.js > ./bundle.js
  6. in index.html, include <script src='bundle.js'></script> just before closing body tag and you're ready to rock.

Example code

<!-- index.html -->
<!DOCTYPE html>
<html>
  <head>
    <title>My Project</title>
  </head>
  <body>
    <h1>My Project</h1>
    <input type="text">
    <script src='bundle.js'></script>
  </body>
</html>

Here, just marked up a basic html5 page, included the bundle.js file generated by browserify. Now we can use this bundle.

// app.js
// NOTE: remember, you code this before running `browserify ./app.js > ./bundle.js`
var $ = require("jquery");
require("jquery-ui-browserify");

$(function() {
  $('input').autocomplete({ source: ['jquery', 'mootools', 'underscore']} );
});

Hope this helps! It worked perfectly for me.

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

Comments

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.