NPM - Guide to Node.js Package Management
- Published on
If you're working with Node.js already or just starting out, you're likely to cross paths with NPM — Node Package Manager. It's the default package manager for Node.js and a critical tool for managing packages in your projects. NPM makes it easy to install, update, and manage dependencies with simple commands, and it helps to ensure that you have the right versions and dependencies properly installed.
What is NPM?
NPM is a package manager for JavaScript, allowing users to install and manage software packages written in JavaScript. As of writing this post, it hosts almost 3 million packages and more than 50 billion weekly package downloads. That's a lot!
What is a package?
Node.js has a very active community. At npmjs.com you can find a public repository of open-source packages that people create and upload for anyone to use. Most of the time you don't need to "reinvent the wheel" and you can use a code that has already written and suits your needs.
Installing NPM
NPM comes bundled with Node.js. When you install Node.js, you automatically get NPM installed on your system. To check if you have it installed and to see the installed version, you can run:
briefly:~ lukasz$ npm -v
10.1.0
Importance of package.json
Every Node.js project that uses NPM should have a package.json
file. This file holds various metadata relevant to the project, which NPM uses to manage project dependencies, scripts, and more. You can create this file manually or by running npm init
which will guide you through creating a package.json file based on your inputs.
Basic NPM Commands
- Installing a package
To install a package, simply use the npm install
command followed by the package name. For example, to install Express, a popular Node.js framework:
npm install express
- Installing packages globally
Sometimes, you might need a package available globally on your system rather than just in a specific project. To install a package globally, add the -g
flag:
npm install -g nodemon
- Listing installed packages
To see a list of installed packages in your project:
npm list
- Updating packages
Keeping your packages updated is crucial. To update a specific package:
npm update express
- Uninstalling packages
If you no longer need a package, you can remove it using the uninstall command:
npm uninstall express
A package manager like NPM is an essential tool across many programming languages, so it's good to have all the basics.
Stay tuned for more content and see you soon.