npm - Node Package Manager[9]

The Node.js documentation mentions npm as a dependency at https://nodejs.org/en/docs/meta/topics/dependencies/#npm. Its documentation is found at https://docs.npmjs.com/.

They brag, rightly, that the Node.js package ecosystem is the world's largest ecosystem of open source online libraries. https://nodejsera.com/nodejs-tutorial-day7-all-about-npm.html is a good video about it. The npm should be installed already with the tools that we looked at in the beginning of this material.

It is essential that we're always using the latest version of this software. Check your version with

npm -v
6.5.0

To get the latest you might update via your package manager, nvm or use npm itself.

nvm install --latest-npm

or

npm install npm@latest -g

The g flag makes it a global install meaning it is not for the current project only.

npm Commands

The npm commands needed at this preliminary stage are

npm init
This command create a project description file containing various configuration parameters,
npm set
Sets up certain defaults used by npm init.
npm install <pkg>
Installs a package from the npm repository.
npm update
Updates packages of the project.
npm outdated
Tells you whether some packages need to be updated.
npm uninstall
Removes packages if no longer needed.

npm init

When we work with node we need to keep track on the files needed for the current project and package.json helps us with that. It is interactively created with npm init

npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (nmltest)
version: (1.0.0)
description: Demo test
entry point: (index.js)
test command:
git repository:
keywords:
author: @phidip
license: (ISC)
About to write to /home/nml/nodeProjects/nodejsexps/nmltest/package.json:

{
  "name": "nmltest",
  "version": "1.0.0",
  "description": "Demo test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "@phidip",
  "license": "ISC"
}


Is this OK? (yes) 

It is non-interactively done as follows. This is based on some configurable best practices parameters.

npm init -y
Wrote to /home/nml/nodeProjects/nodejsexps/package.json:

{
  "name": "nodejsexps",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Just like that, no questions asked. The generated file package.json may be edited and adapted in either case. In a bit we shall see an example of just that.

npm set

You may remember from ancient history when you first started using git, you supplied some useful configuration parameters such as your name and email address. npm offers a similar feature. This is a one time effort.

npm set init.author.name "Niels Müller Larsen"
npm set init.author.email "nmla@iba.dk"
npm set init.author.url "http://dkexit.eu"
npm set init.license "MIT"
npm set init.version "0.9.0"

having done that let us try re-issuing the npm init -y

npm init -y
Wrote to /home/nml/public_html/x15.dk/webdev/package.json:

{
  "name": "webdev",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Niels Müller Larsen <nmla@iba.dk> (http://x15.dk)",
  "license": "MIT"
}

Once you have a package.json file in your project, and after updating it with necessary dependencies you may use npm to install all the dependencies.

npm install

npm install

You may install packages manually with npm in either of two ways

npm install request
npm install -g request

where request is the wanted package name. The former of the two ways installs the package locally in the project folder in a directory node-modules created automatically. The latter way, with the g flag, makes a global installation of the module in the global modules directory thereby being good for all projects except those overriding that with a local node-modules.

Now you may vary that

npm install request@2.88.0

installs exactly that version of request whereas

npm install request^2.0.0

installs the latest version 2 variant available. Running

npm update

npm update

in the project folder will update all packages, or

npm update -g

will perform a global update.

npm outdated

npm outdated

will reveal the necessity of an update. Just after an update, its output should, of course, be empty. The g flag may also be used on that command.

npm uninstall

npm uninstall

followed by a packagename removes a package. The global flag is also applicable here.