dependency management

npm, the package manager that ships with node.

npm is used to manage server-side dependencies, but it can be a great tool for managing front-end code too!

why npm

If you want to reuse code,

When people want to "share the same code between the browser and the server" they typically have some narrow goal in mind of

This use-case is not very common but is easy to explain.


it's more about

the freedom to move code

use the same modules everywhere


It's not strictly sufficient to

One huge problem for repositories that only focus on browser javascript or only focus on server-side javascript is that many modules that only work on one end of the stack have dependencies that work on both ends.

The existence of common javascript libraries that work on either end necessarily entails publishing separate metadata and maintaining version parity for multiple package manager registries when the registries are specialized toward one end of the stack. This is unfortunate. There is a lot of code that is just javascript!

private modules

There are many ways to use npm and node-style modules to manage your frontend and backend code without resorting to

check node_modules into git

npm is really great for managing

pruning duplicates

One thing to watch out for when using node modules is how conflicting versions of dependencies each get their own copy of the module matching the version in their package.json "dependencies" field.

The duplication approach taken by npm solves the incompatible versioning among packages, but can sometimes inflate bundle sizes.

Under normal circumstances on a fresh install, npm will automatically factor out common dependencies into the most common node_modules/ directory, but subsequent npm install commands may introduce duplicates.

In these cases, npm dedupe is your friend!

You can also use browserify --list main.js to show all the files being included in your browserify bundle to make sure there are no extraneous dependencies.

Some libraries like three.js that use instanceof to check instance types require special attention since voxeljs is built on top of both three.js and browserify.

It's important to note that the behavior of npm is fundamentally a feature to ensure the correctness of your programs when dependencies are in conflict. A correct program with more bytes is preferrable to a program with fewer bytes that doesn't work.

quick start guide
Create a github hook to run your tests on every push. Get a browser badge.
writing tests with tape
Write tests with a minimal test api that works in both node and browsers.
testing locally
Run your tests in node and your local browsers.
writing tests with mocha
Run tests in mocha for qunit, tdd, bdd, and exports-style tests.
module-driven development
Write code that is easier to maintain and easier to test with browserify.
dependency management
Use npm to manage your front-end dependencies.
advanced configuration
Read in-depth about the package.json "testling" field.
custom test libraries
Use your own test library by outputting TAP with `console.log()`.