jarofghosts

void "words"

man npm

The npm CLI tool can do a lot of pretty sweet stuff!

In what I predict might be an ongoing series, I want to outline some of the features that might not get used all that often, likely because they just aren’t widely publicized.

I am going to start with what are some of the more well-known options and work my way forward. So! First up:

npm init

This is one I can’t imagine living without. I doubt there are too many people out there who aren’t aware that this is a thing, but there are people born every day who have never seen the Flintstones, so..

What it does

Provides an interactive prompt for generating a package.json file for your project.

Why it’s neat

As a rule of thumb, it is best to avoid manual fiddling in your package.json. This gives you a great start in that you don’t even have to create the file at all. Plus it automatically fills out sections intelligently like where your repo lives (if you have a git remote configured).

Protips

Taking the time to curate your ~/.npmrc file will allow you to provide defaults for prompts like license, name, email, etc. Just use the init. namespace for defining these values. You can even go so far as to provide an entirely different module for generating your package.json file.

More info

npm install --save <module>

This isn’t any big secret. It’s just an option to pass to what is probably the mostly widely-used application for npm: installation of dependencies.

What it does

Updates your package.json to include the specified module as a dependency.

Why it’s neat

Another way to avoid opening up your package.json file! Plus it adds your dependency smartly with a version number and the ^ modifier, if applicable.

Protips

Specify --save-dev instead of --save to add the dependency to your devDependencies. Specify a version or version range to your install command to peg it, i.e. npm install --save through@2.3.4.

More info

npm version (patch|minor|major)

Now we are getting to the slightly more obscure commands. Judging by the number of modules on npm that do exactly this, I get the impression not a lot of people are familiar with it.

What it does

Bumps the version for your project in the package.json accordingly, commits it, and tags the commit.

Why it’s neat

Takes care of one of the more tedious aspects of maintaining a module. Does All The Right ThingsTM as far as tagging releases and updating your repo.

Protips

Use this. If you aren’t familiar with how to know when to bump by which, the simple rule is: * 0.x.x -> Wild Wild West * Beyond that: + Patch versions are for bugfixes that do not alter the API or otherwise add or remove functionality + Minor versions are for adding features that maintain backwards compatibility + Major versions are for backwards-breaking changes.

More info

This is one that comes in handy rather often, that is also rather under-the-radar as far as I can tell.

What it does

Creates a symbolic link from a dependency to a local project on your hard drive

Why it’s neat

No more copying files into sub-project node_modules directories or trying to maintain symlinks on your own. Now you can actively work on a project and test changes and bugfixes without manually managing the mess of mocked or copied dependencies.

Protips

Use npm link from within a project to create a global link. Use npm link <module-name> from within another project to symlink the dependency. Alternatively, use npm link <project-dir> from within the consuming project’s directory to link a dependency from another project directory.

More info