Understanding the Node Package Manager (NPM)
As a Javascript developer, chances are high that you've heard of "NPM." You've probably had to use it to install different packages in your application. But what exactly goes on behind the scene whenever we use the NPM command? How does NPM work exactly?
What is NPM?
NPM, an acronym for Node Package Manager, is the most popularly used package manager for the Javascript programming language - the others being Yarn and Bower. It is the standard package manager for Node.js.
NPM's command-line client makes installing packages so straightforward. All you need to do is simply specify the package you need, and NPM takes care of the rest. What's more impressive? NPM somehow just knows where to find each package. This is made possible by the NPM registry. It is an online database filled with public and private packages. With this registry, NPM can quickly and easily fetch packages from a centralized location, making it an incredibly efficient tool for managing dependencies in your projects.
npm install
What is a Package Manager?
A package manager is a set of software tools that automates the process of managing packages in a consistent and organized manner. When working on a project, you may need to use a variety of third-party libraries and frameworks like React, Redux, and Styled Components. Manually downloading and managing these packages can be a tedious and time-consuming process. You'd have to ensure that you have the correct versions of each package and manage all of their dependencies manually. This can be a headache, especially when you're working on a complex project with multiple packages.
This is where NPM comes in. NPM, or Node Package Manager, is a software tool that simplifies the process of managing third-party packages in your project. With NPM, you no longer have to worry about manually managing dependencies. Instead, you can easily search for and install the packages you need with a simple command.
For example:
npm install styled-components
It will look for styled-components
as the key and find where to download the package from the online database. NPM takes care of downloading and installing Styled Components for you.
NPM automatically manages the dependencies for each package you install, ensuring that your project is running with the correct versions of each package.
Not only does NPM make it easier to manage packages, but it also helps ensure that your project is using the latest and most stable versions of each package. NPM provides regular updates and security patches for each package, ensuring that your project stays up-to-date and secure.
How does NPM make developing easier?
Managing packages are made easier with NPM. Developers can focus more on developing and less on package maintenance since NPM automates package management.
Any Node.js project will always require a package.json file. The package.json file is where you tell the NPM which packages you would like to install, it keeps the information about source control, project metadata, and more.
Whenever you run npm install
, NPM will look at your package.json file and look at what libraries you want to import.
What is the NPM registry?
The NPM registry is a database collection of open-source packages for Node.js, front-end frameworks, and Javascript in general. When you install NPM on your machine, the default registry URL is set to https://registry.npmjs.org/
If you put that link into your browser, you’ll get back a JSON object that gives some information about the NPM registry.
Whenever you run npm install <package-name>
you are telling NPM to look for the package name through the registry URL. This is how NPM can find the packages that you are looking for.
The Basics of NPM
npm init
This command will create apackage.json
file in your project. You can manually add packages to this file, or you can use thenpm install
command.npm init
npm install
This command will look into yourpackage.json
file and install all the packages that are listed in thepackage.json
file.
npm install
npm install <package_name>
This command installs the latest version of the package to yournode_modules
folder.
npm install <package_name>
npm install -g <package_name>
This command installs a package globally to your computer.
npm install -g <package_name>
npm install <package_name>@<version>
Installs the specific version of the package to yournode_modules
folder.
npm install <package_name>@<version>
npm uninstall <package_name>
Uninstalls the latest version of the package.
npm uninstall <package_name>
npm update <package_name>
Updates the specified package for production.
npm update <package_name>
Conclusion
NPM is an important tool that helps developers to focus more on development rather than maintaining packages.
Whenever you want to download a third-party package and install it into your project, you use npm install
and specify the package name. From there, NPM will look through its registry to find the package and download it for you. It will then also manage to hook things up so you can start to use it for development.