In this article, I will be discussing the node package manager (NPM). We'll go over what exactly is NPM, what are its' components, how to install, and its' use cases.
What is NPM?
NPM is a package manager for the javascript programming language. As the world's largest software registry, it is the default package manager for the javascript runtime environment, Node.js. Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. NPM is a tool that allows you to install javascript packages.
The name npm (Node Package Manager) stems from when npm first was created as a package manager for Node.js. All npm packages are defined in files called package.json. The content of package.json must be written in JSON. At least two fields must be present in the definition file which is the name and version of a package.
Components
NPM has three components. These components consist of the npm website, the Command Line Interface (CLI), and an online database of public and private packages called the npm registry. Each of these components possesses its role in the world of NPM. What are these roles? To discover packages, set up profiles, and to manage other aspects of your npm experience, you would use the website. The command-line interface runs from the terminal and this is how most developers interact with NPM. The registry is a large public database of javascript software and metadata surrounding it.
Installation
NPM is installed with Node.js. This means that you have to install Node.js to get npm installed on your computer. When installing a new package, you're creating a new package.json file.
To set up a new or existing npm package you must use the following command:
npm init <initializer>
Initializer in this case is an npm package named create-, which will be installed by npx, and then have its main bin executed – presumably creating or updating package.json and running any other initialization-related operations.
Examples
Create a new React-based project using create-react-app:
$ npm init react-app ./my-react-app
Create a new esm-compatible package using create-esm:
$ mkdir my-esm-lib && cd my-esm-lib
$ npm init esm --yes
Generate a plain old package.json using legacy init:
$ mkdir my-npm-pkg && cd my-npm-pkg
$ git init
$ npm init
Generate it without having it ask any questions:
$ npm init -y
The init command is transformed to a corresponding npx operation as follows:
npm init foo -> npx create-foo
npm init @usr/foo -> npx @usr/create-foo
npm init @usr -> npx @usr/create
Any additional options will be passed directly to the command, so npm init foo --hello will map to npx create-foo --hello.
If the initializer is omitted (by just calling npm init), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use -y/--yes to skip the questionnaire altogether. If you pass --scope, it will create a scoped package.
To install a new package you'll use the following command:
Windows Example
C:\>npm install <package>
Mac OS Example
>npm install <package>
Package.json example
The following example represents what your package.son file looks like after installation.
{
"name" : "foo",
"version" : "1.2.3",
"description" : "A package for fooing things",
"main" : "foo.js",
"keywords" : ["foo", "fool", "foolish"],
"author": "John Doe",
"licence" : "ISC"
}
Why use NPM?
NPM provides plenty of different useful things for developers to use. Because npm has packages of reusable code that won't have to be repeatedly written every time you start a new project. NPM is most commonly used to publish, discover, install, and develop node programs. To make use of the tools (or packages) in node.js, we need to be able to manage them in a useful way. This is where npm comes into play. Npm installs the packages you want to use and provides a user interface to work with them.
Sources
Top comments (1)
Great intro to npm. Since things seem to change rapidly in the software world, GitHub is always sending me warnings about the packages in my projects. I find myself using
npm update
a lot too.