Scott Smith

Blog Tutorials Projects Speaking RSS

8 NPM Tips for Better Node Development

npm is the official package manager for Node.js. It is automatically installed when you install Node.js. You use npm through the command line to install, uninstall, update, and manage packages for your Node.js application. It can also be used to install packages locally or globally to provide functionality on the command line and not necessarily within your Node.js application.

In this article, we will explore some tips related to npm to help super charge your Node.js development.

Tip 1: Install packages locally

1
npm install <package_name>

This is the most basic command that installs a package and any packages it depends on. The package will be installed into a folder named node_modules within the current working directory.

Tip 2: Install packages globally

1
npm install -g <package_name>

If you want to install packages and have them available anywhere, you can use the -g optional flag. This will install packages in {prefix}/lib/node_modules, where {prefix} is usually something like /usr/local.

Tip 3: Install packages as dependencies

1
npm install <package_name> --save

When installing packages, you can pass it the optional flag --save which will tell npm to add the package to the dependencies object within the packages.json file.

1
2
3
4
"dependencies": {
  "express": "^4.0.0",
  "body-parser": "^1.0.2"
}

This is critical when depolying your application to production. In most cases you won’t be deploying your node_modules folder so it will need to know what packages your application depends on so they can be installed.

This is also useful when working with other developers who may get a copy of your source code but not the node_modules folder. By having these dependencies listed, they can simply run npm install from within the application folder to have npm install all the necessary packages.

You can of course update the packages.json file manually, but this will do it for you automatically.

Tip 4: Install packages as dev env dependencies

1
npm install <package_name> --save-dev

Another optional flag you can us when installing packages is --save-dev which will tell npm to add the package to the devDependencies object within the packages.json file.

1
2
3
"devDependencies": {
    "mocha": "^1.18.2"
}

This is very useful because this allows you to install packages for test runners, debugging, and more but not have them install in production.

Tip 5: Install packages as exact version dependencies

1
npm install <package_name> --save --save-exact

In some cases you may want to force an exact package version rather than based on a range or approximate. The --save-exact optional flag used in conjunction with the --save flag will do that for you.

Tip 6: Save time with Nodemon

Now is a good time to seque into some npm packages that will make things much easier when developing Node applications. Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.

You can install it using npm using the following command. I like to install it globally so I can use it for all projects, but you can remove the -g to install it locally instead.

1
npm install -g nodemon

Now instead of using node server.js to run your application, you can use nodemon server.js. It will watch for any changes in your application and automatically restart your server for you.

Tip 7: Enable debugging with Node Inspector

Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools. The really cool thing is that it works almost exactly as the Chrome Developer Tools.

Some of the things you can do with Node Inspector are:

  • Navigate in your source files
  • Set breakpoints (and specify trigger conditions)
  • Step over, step in, step out, resume (continue)
  • Inspect scopes, variables, object properties
  • Hover your mouse over an expression in your source to display its value in a tooltip
  • Edit variables and object properties
  • Continue to location
  • Break on exceptions
  • Disable/enable all breakpoints

Just like Nodemon, you can install it locally or globally. Use the following command to install it globablly:

1
npm install -g node-inspector

Once it is installed, you can run it using the following command. This will start the debugger and open your browser.

1
node-debug server.js

Tip 8: Exclude node_modules from source control

If you are using git for your source control, you will want to add node_modules to your .gitignore file. This will exclude the folder and all installed packages from getting committed to source control. When someone else gets the soruce code, they can simply run npm install from within the application folder to have npm pull down all dependent packages.

Wrap up

These are but a few tips you can use to help make development Node.js applications easier.

If you have tips or tools you use, please feel free to share them in the comments.