Skip to content
Matt BertiMatt BertiMatt BertiMatt Berti

Matt Berti

NPM tricks

Here are some sweet NPM tricks. They might seem pretty obvious to some people. If you're one of them, check out more NPM tricks at devthought.com.

To quickstart a project and build package.json:

$ npm init

Start node using the package.json "start" file (defaults to $ node server.js)

$ npm start

You can also define scripts in the package file:

"scripts": {
    "test": "mocha mytest.js",
    "build": "uglify mycode.js
}

Then run them like this:

$ npm run-script test
$ npm run-script build
$ npm test # shortcut for `run-script test`

View package details:

$ npm view <packagename> [detail(ie 'version')]

Bump package:

$ npm version 1.2.3

It’ll open up your package.json file, change the version to 1.2.3, git add it, git commit it, and git tag v1.2.3 it.

Rebuild a package:

$ npm rebuild <packagename>

This is useful when you install a new version of node, and must recompile all your C++ addons with the new binary. For example, the hiredis package is compiled from C, and if node is updated it will need to be rebuilt.