3

I'm following this short tutorial on getting started with Browserify Getting Started with Browserify. Despite following everything exactly jquery isn't working on the page when bundled,ie. the button element in my app.js code below is not appended to the body. Have checked everything I can think of using chrome dev tools to make sure all is loading ok, which it is.The bundle.js loads fine and my jquery code from app.js is in it. Also the alert() and console.log() output fine so I know the jQuery is specifically being ignored. Tried changing the jquery version to that of the tutorial but still doesn't work. I know the jQuery code itself is fine because it works when I add the code directly on the index page and link to directly to the jquery.js inside node_modules directory. Any ideas what might be the problem?Thanks in advance!

app.js

var $ = require('jquery')
alert("hi from app.js") // this appears in browser on page load

// none of this jQuery currently works using Browserify
var button = $('<button/>').html('click me').on('click', function() {
alert("hi from browserify!")
})
$('body').append(button)

console.log("where is jQuery code ....."); // this logs in dev console

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Browserify Test with jQuery</title>
    <script src="bundle.js" charset="UTF-8"></script>
<!--<script src="node_modules/jquery/dist/jquery.js"></script>-->
  </head>
  <body>
  <!--<script>
    var button = $("<button/>").html("click me").on("click", function() {
    alert("hi from browserify!")
    })
   $("body").append(button)
  </script>-->
</body>
</html>

Here is the code as it appears in the bundle.js file created by Browserify.

......// rest of bundle.js 
},{}],2:[function(require,module,exports){
var $ = require('jquery')
alert("hi from app.js")

var button = $('<button/>').html('click me').on('click', function() {
alert("hi from browserify!")
})
$('body').append(button)

console.log("where is jQuery code .....");

},{"jquery":1}]},{},[2]);

package.js

{
"name": "browserify-test",
"version": "1.0.0",
"description": "Testing Browserify",
"main": "index.js",
"scripts": {
  "build": "browserify app.js -o bundle.js",
  "watch": "watchify app.js -o bundle.js"
},
"author": "MikeM",
"license": "ISC",
"devDependencies": {
"browserify": "^10.2.3",
"jshint": "^2.8.0",
"watchify": "^3.2.1"
},
"dependencies": {
  "jquery": "^2.1.4"
}
}
3
  • So, are you saying that the button is not created and appended to the document? Commented Jun 5, 2015 at 17:15
  • Yes exactly.Sorry if that wasn't clear. When I use Browserify to bundle my app.js. nothing is appended to the body but when I put the code in the index.html and link directly to jQuery it works fine. Commented Jun 5, 2015 at 18:23
  • I updated my question to clarify the problem, being that the button element is not appended to the body as expected when the page loads. Commented Jun 9, 2015 at 3:40

3 Answers 3

4

Try changing var $ = require('jquery') to global.jQuery = require('jquery'). This will attach it to the global object, which is the window in browsers.

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

3 Comments

Gave it a try but still no luck. Thanks for suggestion!
I already had used var $ = require('jquery') for my other functions. So i add another line var $ = global.jQuery. Now all are working fine.
The complete changes: global.jQuery = require('jquery'); global.$ = require('jquery');
1

I would suggest a universal synthesis for changes: Try to change:

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

Comments

0

I had the same issue. Find out I load bundle.js in head, when DOM is not ready. Try to wrap your code in $(function(){...//code here...}) or move bundle.js to the end of body.

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.