This page looks best with JavaScript enabled

A few useful commands in NPM

 ·   ·  ☕ 2 min read

npm is quite simple but immensely powerful for what we do as developers. At the heart of it, NPM just downloads stuff from its own registry on the Internet. But the underlying dependency management makes it an easy-to-use, super efficient tool. Here are some commands that I find useful in day-to-day work.

Install / Uninstall

Of course, I am joking. You would have seen this a billion times.

1
2
npm install nodemon
npm uninstall nodemon

You could follow the shortcut notation

1
npm i nodemon

You can specify the install command without package names so that it can install / refresh all packages listed in package.json.

Initialize folder

You may copy/paste package.json or clone starter repositories to get started.

Or, start from scratch using init.

1
npm init

This command will create a blank folder and a baseline package.json for you to get started from.

Run

You may know all commands by heart, but what about that poor maintenance guy?
It is a good idea to standardize commands and their params through the package.json file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "name": "great-app",
  "version": "0.1.1",
  "description": "Blah blah app",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "test": "node ace test",
    "watch": "blah blah watch"
  },

  "author": "",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "mysql": "^2.16.0"
  }
}

Now anyone can simply do the following to run or watch application.

1
npm run start
List

Often you provide version numbers in package.json, and cannot quite know what was installed. Or, you could have provided something like so -

1
2
3
4
"dependencies": {
    "axios": "~0.18.0",
  },

.. which stands for install anything greater than or equal to the version specified within same minor version. i.e., version can be 0.18.0, 0.18.1, 0.18.9, but not 0.19.0 or 1.0.0).

If you want to find exact version in your folder (or from wherever your npm package is running from) - use list.

1
2
3
npm list axios

` -- axios@0.18.0
Update

You can do an update of specified or all packages.

1
npm update axios

The update follows the conditions specified by you using the semantic versioning.

If the version has changed and you want to do a major or minor update to current version specified in package.json.

Identify outdated packages

Use npm outdated to know which packages have updates available.

1
PS F:\dev\1proj\js\t1> npm outdated

Output -

1
2
3
Package  Current  Wanted  Latest  Location
axios     0.18.1  0.18.1  0.19.0  client

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things