Using Node.js modules in the browser

At the beginning of this chapter, we discussed creating universal modules that can run under Node.js or in the browser. There is another way that we can allow our code to run in both environments.

Browserify (http://browserify.org/) allows you to make use of Node.js modules in the browser. It bundles up your code together with its dependencies. It also provides browser-compatible shims to emulate Node.js built-in modules.

You can install Browserify via npm:

> npm install -g browserify

Browserify is typically used to package applications. For example, if we wanted to package our demo usage of autotoc from the previous section, we could run:

> browserify demo.js -o bundle.js

Browserify will create a single JavaScript ...

Get Learning Node.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.