The npm(Node Package Manager) is the largest software registry in the world. It is an open source library that helps developers around the world install, share and manage packages.
It is the default package manager for Node.js but can also be used for front-end projects and other JavaScript environments.
Each npm package is essentially a reusable code, ranging from utilities to libraries and frameworks. With npm, you don’t have to write code from scratch.
Think of it as a free store that contains tools for your programming projects.
What is an NPM Package?
As stated earlier, packages are a bundle of code that do specific things, like changing the font in your webpage or connecting to a database.
Packages are files or directories that are described by a package.json
file. This file mainly contains information about the package, like the name of the package, version, description, and the author.
Think of the package.json
file as the ID of your package. Without it, the package won’t have the necessary information to help it run, like its dependencies.
Apart from the package.json
file, a package contains other files such as:
JavaScript file - for the actual function of the package.
Dependencies - Other packages that the package needs to work.
Documentation - Contains instructions on how to set-up the package.
Configuration files - These are settings that tell the package how to behave in different situations.
Look at it this way: you bought a car, you are the driver, there is an engine, there is also a manual, and there’s the driver’s preference (mirror adjustment, sit position, and so on).
In the case of packages, the JavaScript file is the driver, the driver tells the car what to do and where to go.
The engine is the dependency, they are components that the car needs to be able to function.
The manual is the documentation, it contains information on the car and what does what in the car.
Setting the position of the seat, maybe because of the height of the driver or other reasons is the same as configuring the packages depending on the situation.
How To Install The Latest Version of NPM
To install npm, you must install Node.js
first and the CLI (Command Line Interface). Now, you can either install Node.js
using a node installer or with a Node version manager like nvm. It is recommended to use nvm for the installation.
Once you install Node.js
, npm comes with it.
I assume you now have Node.js
installed, I’ll proceed with installing the latest version of npm.
Installing npm is pretty easy, just run the following command on the command line: npm install -g npm
If you want to check the version of your npm and node, just run the following commands on your CLI:
npm -v
for npm and node -v
for node.js.
How To Install Packages
There are two ways of installing npm packages: Globally, and locally.
Installing npm globally means it can be used by any of your projects, and from any directory. Global packages are mostly packages that are meant to be run on your command line.
While installing it locally means it is only available to the specific folder that it is installed in. This is used when you just need the package for that particular project like when installing packages for their utility.
Let’s start with global installment.
To install a package globally, just run this code like the previous one in your command line:
npm install -g package-name
.
The -g
there tells the computer that this is a global package.
To install a package locally, go to the project’s terminal and run the command:
npm install package-name
.
What are npm Commands and Aliases?
We now know what npm is and how to install packages, what about how to use npm? That is what we will be talking about next.
npm comes with a list of commands and a feature that helps developers define custom names for their dependencies called aliasing.
Below I’ll give you a list of some of those commands and also tell you how to alias your npm package.
npm Commands
These are some of the more common npm commands:
npm init
- This creates apackage.json
file.npm install package-name
- Installs a specific package locally.npm install -g package-name
- Installs a global package.npm install
- Installs packages listed in apackage.json
file.npm uninstall package-name
- Uninstalls a package.npm uninstall -g package-name
- Uninstalls global packages.npm update
- This updates all npm packages to their latest versions.npm list
- Lists all the npm packages installed in a project.npm outdated
- Checks for outdated npm packages.npm audit
- Checks for security vulnerabilities in dependencies.npm audit fix
- Fixes the vulnerabilities found in the audit.npm run script-name
- Runs a custom script defined in thepackage.json
file.npm start
- Runs the start script defined in apackage.json
file.npm build
- Builds a package.npm publish
- Publishes an npm package.npm help
- Displays a list of npm commands.
How to NPM Package Aliasing
Npm aliasing is a process of defining a different name for the package you are installing to be used in your code based on your preference.
For example, you may want the name of the packages to be related to your project, you want to be clear or you want to reduce the risk of naming conflicts. Aliasing helps solve these problems.
We’ll learn how to alias a package with the following steps:
Open the project and the project’s terminal.
Run this code in the terminal:
npm alias-name@npm:package-name
.After the first two steps you can import or require() the package into your project using the alias you just created.
Conclusion
In conclusion, npm is a vast registry and package manager that simplifies the process of installing, sharing and managing packages. By understanding the process of installing, sharing, managing packages and aliasing, developers can improve their productivity and workflow. This article, has provided some of the basics of npm to get started.
That is all for now, I hope this article helped enhance your understanding of npm and npm packages.