NPM (Node Package Manager) is a powerful tool that comes bundled with Node JS, allowing us to manage and install external libraries, modules, and packages, known as dependencies, for our Node JS projects. It is the largest package registry in the world, hosting thousands of open-source packages that we can use to enhance the functionality and efficiency of our applications.
Managing dependencies manually can be very difficult. Npm simplifies this process by automatically handling package installations, version management, and dependency resolution. It also enables easy sharing and reuse of code among us, fostering collaboration and code quality improvement.
In simpler terms, it’s a warehouse of software you can easily plug into your projects, so you don’t have to write everything from scratch. These pieces of software (called packages or modules) can range from simple helper libraries like Lodash to more complex frameworks like React.
To use npm, you first need to install Node JS on your computer. You can download it from the official Node JS website. npm comes bundled with Node JS, so you get it automatically when you install Node JS.
To see how to install Node JS on your computer, click here.
To check if npm is installed, open your terminal or command prompt and type:
npm -v
Once you’ve got Node JS and npm installed, you can create a new npm project. In your terminal or command prompt, navigate to your project’s directory, and run:
npm init
You will be prompted to enter details about your project (name, version, description, entry point, etc.). You can either fill them manually or press Enter to accept the defaults. This will initialize a new npm project and creates a package.json
file in your project directory.
The package.json
file is a crucial part of any npm-based project. It’s like a blueprint of your project that contains metadata about the project itself and the list of dependencies it needs to run correctly.
{"name": "my-awesome-project","version": "1.0.0","description": "A description of my awesome project","main": "index.js","scripts": {"start": "node index.js"},"dependencies": {"lodash": "^4.17.20"}}
Dependencies are external modules that your project relies on. When you install a package using npm, it’s listed in your package.json
file, along with its version number. This way, anyone who needs to run your project can see which packages they need to install.
To install a package, you can use the npm install
command, followed by the package’s name. For example, if you want to install the express framework, you can use the following command:
npm install express
Once the command runs, npm fetches the express package from the npm registry and saves it in the node_modules
folder in your project directory. It also adds express to the dependencies
list in your package.json
file.
Now, when you or someone else needs to install all the dependencies of your project, all they have to do is run npm install
without any arguments. npm will look at the package.json
file and install all the packages listed under dependencies
.
You can specify dependencies in different categories: dependencies
, devDependencies
, and peerDependencies
.
Dependencies: These are packages required for your application to function correctly. When users install your project, npm will also install these dependencies automatically.
DevDependencies: These are packages required for development purposes only, such as testing frameworks or building tools. When others install your project, devDependencies won't be installed automatically.
Peer dependencies: These are packages that your project requires the consumer to install. They are not automatically installed but must be explicitly installed by the users of your package.
To save a package as a dependency
or devDependency
, use the --save
or --save-dev
flag, respectively, when installing the package:
npm install package-name --save # To save as a dependencynpm install package-name --save-dev # To save as a devDependency
To update a package, you can use the npm update
followed by the package’s name:
npm update express
If you no longer need a specific package, you can uninstall it using:
npm uninstall express
This will remove the package from both node_modules
and your package.json
file.
Npm uses a system called semantic versioning or semver. It’s a 3-part number like 5.12.0
. The parts from left to right are major, minor, and patch versions. Npm uses the caret (^
) and tilde (~
) to denote which kinds of updates your package can accept.
^
: updates minor versions (e.g., 1.0.0
to 1.1.0
)
~
: only updates patch versions (e.g., 1.0.0
to 1.0.1
)
Free Resources