So, you’ve just started with Node.js, and you keep hearing about npm (Node Package Manager).
But what is npm?
Imagine npm as a giant supermarket for JavaScript packages.
- Want to build a web server? There’s a package for that!
- Need to scrape a website? There’s a package for that too!
- Looking for a package that tells dad jokes? You guessed it—npm has one!
In this guide, you’ll learn:
- What is npm and why do you need it?
- How to install, update, and remove packages.
- The difference between global and local installations.
- Some fun and useful npm commands.
Let’s get started!
What is npm?
npm (Node Package Manager) is the default package manager for Node.js.
It allows you to install, update, and manage JavaScript libraries (a.k.a. "packages" or "modules").
npm is included with Node.js, so if you’ve installed Node.js, you already have npm!
Check if npm is installed:
npm -v
Output:
9.8.1 (Your version may differ)
Congrats! You’re ready to use npm!
Installing Packages with npm
1. Installing a Package Locally (Project-Specific)
npm install package-name
Example: Installing express
npm install express
What happens?
- npm creates a
node_modules/
folder (if it doesn’t exist). - It downloads and saves
express
insidenode_modules/
. - It updates
package.json
(if it exists).
2. Installing a Package Globally (Available Everywhere)
npm install -g package-name
Example: Installing nodemon
globally
npm install -g nodemon
What happens?
- The package is installed system-wide.
- You can use it from any directory.
When to install globally?
- CLI tools like
nodemon
,http-server
, ornpm-check-updates
.
Understanding package.json
What is package.json
?
- It’s like a manifest file for your project.
- It lists dependencies, scripts, and metadata.
Creating package.json
(The Easy Way)
npm init -y
Example package.json
file:
{
"name": "my-app",
"version": "1.0.0",
"description": "My first Node.js app",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
Installing Different Types of Dependencies
1. Installing a Production Dependency (dependencies
)
npm install lodash
Adds "lodash"
under "dependencies"
in package.json
.
2. Installing a Development Dependency (devDependencies
)
npm install jest --save-dev
Used for testing tools, linters, or build tools.
3. Installing an Exact Version
npm install moment@2.29.4
Installs a specific package version.
4. Installing Multiple Packages at Once
npm install express mongoose cors dotenv
Installs multiple packages in one command.
Updating and Removing Packages
1. Updating All Packages
npm update
Updates all dependencies to the latest minor/patch versions.
2. Updating a Specific Package
npm update lodash
Updates only lodash
.
3. Removing a Package
npm uninstall package-name
Example:
npm uninstall express
Removes the package from node_modules
and package.json
.
Checking Installed Packages
1. List Installed Packages (Locally)
npm list
Shows installed packages inside your project.
2. List Installed Packages (Globally)
npm list -g --depth=0
Shows all globally installed packages.
3. Find Outdated Packages
npm outdated
Lists packages that have newer versions available.
Running Scripts with npm
1. Adding Custom Scripts to package.json
Modify package.json
:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
2. Running the Script
npm run dev
Executes nodemon index.js
.
Fun and Useful npm Packages
1. Create Cool ASCII Banners
npm install cowsay
Use it in your project:
const cowsay = require('cowsay');
console.log(cowsay.say({ text: "I ❤️ Node.js!" }));
Output:
I ❤️ Node.js!
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
2. Get Random Dad Jokes
npm install one-liner-joke
Example:
const joke = require('one-liner-joke');
console.log(joke.getRandomJoke().body);
Laugh while coding!
Conclusion
Now you’re an npm master!
- You know how to install, update, and remove packages.
- You understand global vs. local installation.
-
You can write custom scripts in
package.json
.
Next step? Try exploring fun npm packages like chalk
, figlet
, or ora
to make your CLI apps more exciting!
Happy coding!
0 Comments