npm is the package manager for the Node.js platform. It helps you to download (install) modules so that node can find them during runtime. npm also manages dependency conflicts intelligently.

npm has lots of commands and options and sometimes it is very hard to remember all of them. So I decided to create this post with most handy npm commands to have a quick reference page in the future.

I documented npm working with version 1.3.11 (npm -v)

How to get help

As a mature product, npm has lots of documentation. The first command to run would be npm (without any arguments). This will show you a list of commands and other help topics.

npm <cmd> -h - this displays quick help on the given command; very handy when you forget how to do something or you forget syntax or parameters.

In order to get more detailed documentation run npm help <term>.

In order to get a full usage info run npm -l. That will display all commands and all its parameters e.g.

C:\>npm -l
Usage: npm <command>
where <command> is one of:
    adduser     npm adduser
                Then enter stuff at the prompts
    bin         npm bin
                npm bin -g
                (just prints the bin folder)
    bugs        npm bugs <pkgname>
...

Initialize a package.json file

In order to initialize a package.json file automatically, run npm init. It will ask you a few questions (like package name, package version etc.) and then ask you for verification and confirmation, and will create a package.json file in the current folder.

It is also possible to update dependencies section package.json of file during module installation. Installing module with option --save (e.g. npm install module --save) will automatically add the module and its current version number to package.json.

Find out installed module version

In order to find out a version of an installed module run npm list <module>. If you omit module name, all installed modules will be displayed (in the following format <module_name>@<version>) e.g.

C:\projects\npm-test>npm list underscore
C:\projects\npm-test
└── [email protected]

C:\projects\npm-test>npm list
C:\projects\npm-test
├── [email protected]
└── [email protected]

I you have more modules and those modules have some dependencies then running npm list will give you entire hierarchy of modules e.g. listing modules with express module present:

C:\projects\npm-test>npm list
[email protected] C:\projects\npm-test
├─┬ [email protected] extraneous
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├── [email protected]
│ │ │ └── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

If you would like to see modules only up to a specific level run npm list with --depth=level parameters (where level 0 is root level, level 1 is one additional level etc.).

C:\projects\npm-test>npm list --depth=0
[email protected] C:\projects\npm-test
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]