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
└── underscore@1.5.2

C:\projects\npm-test>npm list
├── underscore@1.5.2
└── underscore.string@2.3.3

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
npm-test@0.0.1 C:\projects\npm-test
├─┬ express@3.4.7 extraneous
│ ├── buffer-crc32@0.2.1
│ ├─┬ commander@1.3.2
│ │ └── keypress@0.1.0
│ ├─┬ connect@2.12.0
│ │ ├── batch@0.5.0
│ │ ├── bytes@0.2.1
│ │ ├─┬ multiparty@2.2.0
│ │ │ ├── readable-stream@1.1.9
│ │ │ └── stream-counter@0.2.0
│ │ ├── negotiator@0.3.0
│ │ ├── pause@0.0.1
│ │ ├── qs@0.6.6
│ │ ├── raw-body@1.1.2
│ │ └── uid2@0.0.3
│ ├── cookie@0.1.0
│ ├── cookie-signature@1.0.1
│ ├── debug@0.7.4
│ ├── fresh@0.2.0
│ ├── merge-descriptors@0.0.1
│ ├── methods@0.1.0
│ ├── mkdirp@0.3.5
│ ├── range-parser@0.0.4
│ └─┬ send@0.1.4
│   └── mime@1.2.11
├── underscore@1.5.2
├── underscore.string@2.3.3
└── when@2.7.0

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
npm-test@0.0.1 C:\projects\npm-test
├── express@3.4.7
├── underscore@1.5.2
├── underscore.string@2.3.3
└── when@2.7.0