The npm registry is the de-facto standard for hosting JavaScript packages. Those packages have a particular format and are called npm packages.
Therefore, in the JavaScript ecosystem, a package manager is a command line tool for installing npm packages – from the npm registry or other sources.
The most popular package manager is called npm and comes bundled with Node.js. Its name originally stood for “Node Package Manager”. Later, when npm and the npm registry were used not just for Node.js packages, the definition was changed to “npm is not a package manager” (source).
There are other popular package managers such as yarn and pnpm. All of these package managers use the npm registry by default.
We use npm via the shell command npm
which provides several subcommands such as npm install
.
We can use the npm
command to explain itself: On one hand, there is the option -h
which can be used after npm
and after npm commands. It provides brief explanations:
npm -h # brief explanation of `npm`
npm <cmd> -h # brief explanation of `npm <cmd>`
On the other hand, there is the command npm help
which provides longer explanations:
npm help # brief explanation of `npm` (same as `npm -h`)
npm help npm # longer explanation of `npm`
npm help <cmd> # longer explanation of `npm <cmd>`
npm help <topic> # longer explanation of <topic>
Help topics include:
folders
npmrc
package.json
The official npm documentation is also available online.
These are a few common commands:
npm init
“initializes” the current directory to be a package. That is, it creates the file package.json
in it. This command is explained in §14.3.1 “Setting up the package’s directory”.
npm install
installs npm packages globally or locally. It is explained in §13 “Installing npm packages and running bin scripts”.
npm publish
publishes packages to registries: It either creates a new package or updates an existing package. It is explained in §14.5.3 “npm publish
: uploading packages to the npm registry”.
npm run
(which is short for npm run-script
) executes package scripts. Package scripts are explained in §15 “Running cross-platform tasks via npm package scripts”.
npm uninstall
removes a package that was installed globally or locally.
npm version
prints the object process.versions
which records the versions of various components of Node.js and npm:
{'my-package': '1.0.0', // current package
npm: '8.15.0',
node: '18.7.0',
v8: '10.2.154.13-node.9',
uv: '1.43.0', // libuv
···tz: '2022a', // version of tz database
unicode: '14.0', // version of Unicode standard
··· }
npx
lets us run bin scripts in packages without installing them. It is described in §13.4 “npx
: running bin scripts in npm packages without installing them”.
The npm documentation has a list of all npm commands.
Many npm commands have abbreviations – for example:
Short | Long |
---|---|
npm i |
npm install |
npm rm |
npm uninstall |
npm run |
npm run-script |
For each npm command it describes, the npm documentation also lists all of its aliases (including abbreviations).