Some useful npm commands

Node looks for the module in a node_modules subdirectory, using a search hierarchy that includes searches in:

  • a node_modules subdirectory local to the application (/home/user/projects/node_modules)
  • a node_modules subdirectory in the parent subdirectory to the current application (/home/user/node_modules)
  • continuing up the parent subdirectories, looking for a node_modules, until top-level (root) is reached (/node_modules)
  • finally, looking for the module among those installed globally

So if you’re testing a new version of a module, and you’ve installed in locally, relative to your application:

npm install shortid

It will be loaded first, rather than the globally installed module

npm install -g shortid

You can see which module is loaded using the require.resolve() function

console.log(require.resolve('shortid'))

You can install a module that’s in a folder on the filesystem, or a tarball that’s either local or fetched via a URL

npm install http://myserver.com/foo.tgz

If you want to install a version of the module that hasn’t yet been uploaded to the npm registry, you can install directly from the Git repository

npm install https://github.com/visionmedia/express/tarball/master

If the package has versions, you can install a specific version

npm install foo@0.1

The following command tells npm to check for new modules, and perform an update if any are found

npm update

Or you can update a single module

npm update shortid

If you just want to check to see if any packages are outdated, use the following

npm outdated

List installed packages and dependencies with list, ls, la, or ll

npm ls

To see which modules are installed globally, use

npm ls -g

or list only 1 level depth

npm list -g --depth=0

Show path to libs

npm root -g

You can also search for a module using whatever terms you think might return the best selection

npm search shortid

The npm documentation recommends you create a package.json file to maintain your local dependencies. To create a default package.json file in the project directory, run the following command

npm init --yes
comments powered by Disqus