January 2016
Intermediate to advanced
278 pages
4h 53m
English
With Browserify we can use Node modules directly in the browser. This means that you can build your projects with the power of the npm package manager and the Node module syntax exposed in the previous sections. Then Browserify can take your source code and apply some transformations to be able to run your code in the browser environment.
A very simple module that exposes an object with a method that prints a hello message can be written as a Node module:
// hello.js
module.exports = {
sayHello: function(name) {
name = name || 'world';
console.log('hello', name);
}
}This simple piece of code can be loaded from another script as shown next:
// main.js var hello = require('./hello'); hello.sayHello(); // hello world hello.sayHello('abiee'); ...Read now
Unlock full access