NPM Workflow, Tips and Tricks

  1. Start your project (or module) off right

    npm init

    I'm surprised to hear how few people use this command. It is the de facto way to kick off a new project that uses npm: whether it's an npm module you plan to publish or just a project that will use npm modules. Use it.

  2. Automate Common Tasks with npm scripts

    npm start, npm test, and npm run <your custom job>

    The build tool wars wage on, with grunt, gulp, broccoli, etc. When I hear people saying "just run grunt build, or start your project with gulp serve", I wonder if they know about npm scripts at all.

    Quite frankly, npm scripts should be the public API of your project automation.

    npm start should include the command to start the server and/or watch for changes and rebuild (preferably both). This is where your [grunt|gulp] serve should go.

    npm test should be run consistently to automate test coverage

    npm prepublish should run any compilation steps (i.e. to convert coffeescript into javascript before publishing to npm)

    npm run <anything-you-want> should be used to run any custom tasks that make your project easier to work on, like version-bumping, building (without running server), remote server tasks, etc.

    When creating software, common sense tells us to depend on things that change less frequently (think public APIs) over things in flux (implementation details). So why do we admonish running build-library-specific commands in our documentation instead of using the established standard, which is NPM?

  3. Save valuable keystrokes

    npm install --save <some-module> just becomes too much typing.

    npm i -S <some-module> will get you the same thing.

    npm i -D <some-module> will get you the same thing except with the --save-dev option instead of --save

    Even better, you can just configure NPM to always save modules as you install them: npm config set save true

    npm i <some-module> is all you need after that.

  4. Keep your node_modules slim

    npm prune will clean out unneeded modules from your node_modules directory. If left to my own devices, this will get run twice a year.

    There's no need to let your faulty memory disservice you again here: prefix your test and prepublish npm scripts with npm prune && <rest-of-command> to ensure this happens regulary, and especially before you publish to the registry.

  5. When all else fails: Getting Help

    npm help, or even better: npm -l

    This is a good place to start: it lists all the top-level commands, and some very basic info. Running the alternative npm -l will give you much more info on subcommands, and what each command expects for arguments.

    npm help 7 config

    I thank the NodeUp podcast specifically for this hidden gem. Using the easier to find npm help config will show you the help for NPM-CONFIG(1), or in manpage speak, the help page for the user-level command config. It turns out that is not so helpful for npm, since the only things you can do with it are get, set, list, delete, and edit entries (much like shell variables). However, what you probably want is all the helpful configuration options available to you via the commandline args and config file ~/.npmrc, and that is what you'll find at npm help 7 config