npm is the standard package manager for Node.js.
In January 2017 over 350000 packages were reported being listed in the npm registry, making it the biggest single language code repository on Earth, and you can be sure there is a package for (almost!) everything.
It started as a way to download and manage dependencies of Node.js packages, but it has since become a tool used also in frontend JavaScript.
Table of Contents
An introduction to npm
- npm is Node’s package manager. It’s a repository of hundreds of thousands of useful pieces of code that you may want to integrate with your Node project.
- npm also has a command line tool that lets us easily install, manage and run projects.
- npm is written entirely in JavaScript(JS) and was developed by Isaac Schlueter. It was initially used to download and manage dependencies, but it has since also used frequently in frontend JavaScript.
- npm can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. In addition to plain downloads, npm also manages versioning, so you can install any version, higher or lower according to the needs of your project. If no version is mentioned, the latest version of the package is installed.
- You can have a more in depth overview of NPM in their documentation.
Yarn and pnpm are alternatives to npm cli. You can check them out as well.
How to use npm:
Installing all dependencies
If package.json file exists in your project root directory, all you need to do is use this command
npm install
This command will initialize the node_modules folder and install all packages that the project needs.
Installing a single package
If you just need to install a single package, you can use this command
npm install <package-name>
Often you’ll see more flags added to this command:
- –save-dev installs and adds the entry to the package.json file devDependencies
- –no-save installs but does not add the entry to the package.json file dependencies
- –save-optional installs and adds the entry to the package.json file optionalDependencies
- –no-optional will prevent optional dependencies from being installed
Shorthands of the flags can also be used:
- -S: –save
- -D: –save-dev
- -O: –save-optional
Updating packages
And if you need to update the installed packages, hit
npm update
All packages will be updated to their latest versions.
Similarly, if you just need to update a single package, all you need to do is
npm update <package_name>
Note: By default, packages are installed in the local scope. If you need to install the package at global scope, you need to mention the flag -g
npm install <package_name> -g
This will install the package at the global scope in the system directory.
Versioning
npm also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.
Running Tasks
The package.json file supports a format for specifying command line tasks that can be run by using
npm run <task-name>
Initialize a Project Using npm init
Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages.
Okay, so let’s start!
- npm init
npm init is the first command you need to know to create a Node project. It will ‘initialise’ your project by adding a file called package.json in the root directory.
npm will ask you some questions in your terminal after you run the command — your answers will be pre-filled into the package.json.
Don’t worry, you can change your answers later by editing package.json manually.
When you are in your desired directory, open the CLI, and use this command:
npm init
A text utility appears and you are asked to enter a few details to create a basic package.json file such as package name, version, description, author, etc.
Enter the details of the package as you please. The defaults will be mentioned in parentheses like this →
package name: (test-package) version: (1.0.0) description: This is a test package . . . Just hit Enter if you want the default options.
A basic package.json will be created looking something like this →
{ "name": "test-package ", "version": "1.0.0", "description": "This is a test package", "main": "index.js", "author": "ashramtech", "license": "ISC" }
Obviously, you can change the package.json file later if you want.
Where does npm install the packages?
When you install a package using npm you can perform 2 types of installation:
- a local install
- a global install
By default, when you type an npm install command, like:
npm install cowsay
the package is installed in the current file tree, under the node_modules subfolder.
As this happens, npm also adds the cowsay entry in the dependencies property of the package.json file present in the current folder.
A global installation is performed using the -g flag:
npm install -g cowsay
When this happens, npm won’t install the package under the local folder, but instead, it will use a global location.
Where, exactly?
The npm root -g command will tell you where that exact location is on your machine.
Conclusion:
Now you’ve got a basic idea about what npm and its important commands. Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages. We will cover in next article How to publish your own package to the npm registry.