NPM Workflow, Tips and Tricks
-
Start your project (or module) off right
npm initI'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.
-
Automate Common Tasks with npm scripts
npm start,npm test, andnpm 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 withgulp 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 startshould include the command to start the server and/or watch for changes and rebuild (preferably both). This is where your[grunt|gulp] serveshould go.npm testshould be run consistently to automate test coveragenpm prepublishshould 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?
-
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-devoption instead of--saveEven better, you can just configure NPM to always save modules as you install them:
npm config set save truenpm i <some-module>is all you need after that. -
Keep your
node_modulesslimnpm prunewill 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
testandprepublishnpm scripts withnpm prune && <rest-of-command>to ensure this happens regulary, and especially before you publish to the registry. -
When all else fails: Getting Help
npm help, or even better:npm -lThis is a good place to start: it lists all the top-level commands, and some very basic info. Running the alternative
npm -lwill give you much more info on subcommands, and what each command expects for arguments.npm help 7 configI thank the NodeUp podcast specifically for this hidden gem. Using the easier to find
npm help configwill show you the help forNPM-CONFIG(1), or in manpage speak, the help page for the user-level commandconfig. 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 atnpm help 7 config