NPM “Node Package Manager” is one of the tools that you will surely use most frequently in the development environment.
NPM is a package manager that is mainly used in the Node.js and JavaScript development environment. NPM was created to facilitate the installation, management, and updating of dependencies for Node.js projects.
Since Node.js has become one of the most important tools for software development, NPM is also a utility that you will use very frequently. Therefore, it is advisable to learn how to use it.
Main features of NPM
Package repository
NPM provides a public online repository that hosts a huge amount of open-source software packages. Currently, the NPM Registry contains millions of packages available for download and use.
Installation and management of packages
NPM facilitates the installation and management of Node.js packages in our projects. To install a package, we simply run a command in the command line and NPM will take care of downloading the package and all its dependencies automatically. In addition, we can also easily update or delete packages.
Scripts
NPM allows you to define custom scripts. These scripts allow us to automate common tasks, such as running tests, compiling code, or generating documentation.
Basic usage of NPM
Let’s see some basic NPM commands to be able to use it in Node.Js projects.
Creating a Node.js project
To create a new Node.js project, we run the following command.
npm init
This creates a package.json
file, which is the starting point for creating a project in Node.js.
Installing dependencies
To install all the packages listed as dependencies in the package.json
file, simply do:
npm install
The libraries are downloaded and stored in the node_modules
folder.
Adding a package
To install a package, use the following command:
npm install package-name
This command will download the package and all its dependencies to your project directory.
Updating packages
If you want to update a package to the latest available version, use the following command:
npm update package-name
Removing packages
If you no longer need a package in your project, you can delete it using the following command:
npm uninstall package-name
Running scripts
If you have defined scripts in your package.json
file, you can run them using the command:
npm run script-name