Flite Careers

Underappreciated NPM Commands Developers Should Know About

npm, the package manager that comes bundled with node.js, is generally described as “awesome”. While it was originally a separate project, npm has been bundled with node.js for the past few releases and the number of npm packages available in the npm registry has exploded.

While there’s a well-documented workflow to interacting with npm packages (npm install, npm test, npm publish, rinse and repeat) there are several powerful npm commands that are very useful but less commonly used.

1. The power of npm scripts

It’s generally known that the scripts member of a package.json file allows developers to specify actions that npm can execute. For instance, if you have a scripts member in your package.json file that looks like this:

"scripts": {
    "start": "node server.js",
    "test": "grunt test"

The command npm start will execute node server.js and npm test will execute the grunt test command (or any other shell script). It’s very useful for defining a common interface to start and test node.js packages across projects.

What’s less well-known, however, is that the scripts member supports over dozen other actions that can execute before and after other npm commands. For example:

"scripts": {
    "preinstall": "./bin/custom-script.sh"

Before npm install is run, the ./bin/custom-script.sh file is executed. If the custom script returns a non-zero exit code, the npm install command aborts.

Other actions include postinstall, poststart, prepublish and pretest.

This is very nice for coffeescript projects. You can define a prepublish script that compiles your *.coffee files to *.js and your app is ready to run in node.js. All users have to do is run npm install as they normally would.

Alternatively, you could have prestart script that uses grunt to minify and concatenate *.js files before starting the web server. This is particularly useful for avoiding including minified and optimized resources in source control, not to mention making it easier to deploy to cloud providers like heroku and nodejitsu.

2. Faster package development with npm link

npm link is an essential tool when developing npm packages that you’d like to test in other projects on your system. It creates a globally-installed symlink for a package folder and then allows you to use that globally-installed symlink in another project. As you change the linked package, the other package that gets the changes instantly. It’s great for package development.

As the documentation states, it’s a two step process:

  • Run npm link in a package folder (typically the package you’re developing).
  • In other package folder (typically the one you want to test with your in-development package) run npm link <your development package name>.

That’s it.

3. Keeping track of old dependencies with npm outdated

The npm package world moves very fast. It’s easy to write a new package and then have its dependencies get out-of-date. The npm outdated command does exactly what it says – it reads your package.json file and tells you which of your dependencies have new versions.

It’s a simple idea but one that I’ve tried to work into my development workflow more often. It’s a good sanity check to make sure I have the fairly recent versions installed of my package’s dependencies.

4. Know your npm aliases

How many times have you typed npm install after cloning a git repository? That’s an entire 11 characters! Fortunately, there’s a shortcut: npm i does exactly the same thing.

The list of aliases is longer than you might expect – there’s no less than four ways you can uninstall a package: npm rm, npm unlink, npm r, npm remove and npm un.

There’s even a helpful npm isntall alias that maps to npm install.

Here’s the complete list.

5. Shell completion with npm completion

npm aliases are nice, but what’s even better than aliases is not typing text at all. npm completion is one of those commands I wish I knew about months ago – it prints a shell script snippet that adds shell completion for npm commands.

For example, typing npm i<tab> then gives you all of the wonderful npm command possibilities that start with i:

sh $ npm i [press tab]
i        info     init     install

To install npm shell completion, it’s just a matter of adding the script npm completion outputs to your shell init file. Or, even better, add it to your dotfiles repository.

npm has great man pages, and with autocompletion it’s even easier to learn new interesting commands.

Bonus: Happy Holidays from npm!

Finally, perhaps the one command every node.js developer should know about: npm xmas

Open your terminal and run this command immediately.