This post will show you how to easily download and include 3rd party libraries in your Browserify bundle.
Let’s start by assuming the following project structure
We have our
main.js file where we will require our local module (
module.js) and our jQuery library
One of the advantages of using Browserify is that you don’t have to use bower or download your dependencies manually. You can just install everything with npm.
To install jquery:
npm install jquery
Since jQuery has been installed with npm, we can require it in our client code like we would do in Node: by simply using the name of the library.
var $ = require('jquery');
As a bonus here is the correct way to require local modules.
When requiring a local module, the path should always start with:
./representing the current directory
../representing the parent directory
In our project example, to require
main.js, we have to use
var module = require('./module');
Happy bundling !