- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js npm (Node Package Manager)
npm (Node Package Manager) is much more than just a tool; it is the world’s largest software registry and the default package manager for Node.js. When you install Node.js, npm is included automatically. It acts as a bridge, allowing you to easily download, share, and manage millions of code libraries (packages) created by other developers so you don't have to reinvent the wheel for every project.
Key Features of npm
- Package Management: Access a massive registry of over two million packages to add functionality like authentication, data manipulation, or UI components.
- Dependency Management: npm keeps track of every library your project needs (and the specific versions of those libraries) in a file called
package.json. - Script Runner: It allows you to create custom aliases for long terminal commands, making it easy to start servers, run tests, or build your application for production.
- Custom Package Creation: You can bundle your own useful code and publish it to the npm registry for your team or the entire world to use.
.js files. This makes your project reproducible and easier for other developers to set up.
Using npm
1. Verify npm Installation
Since npm comes bundled with Node.js, you usually don't need a separate installation. It is a good habit to check your version to ensure everything is working correctly and to see if you need an update.
- Open your terminal or command prompt and type:
npm -v
Output (Example):
9.5.1
2. Initialize a Node.js Project
Before installing any libraries, you need to "initialize" your project. This creates a package.json file, which serves as the "manifest" or "instruction manual" for your project.
- To create a
package.jsonwith default settings instantly:
npm init -y
Output:
- A
package.jsonfile is generated in your current folder. This file will eventually list every library you install.
npm init before installing packages. If you don't have a package.json file, npm won't be able to track your dependencies, making it nearly impossible to share your project with others.
3. Install a Package
Packages can be installed in two main ways: locally (for a specific project) or globally (for your entire computer).
- Local Installation: Use this for libraries your project needs to run (like Express or Lodash).
npm install lodash
Output: A node_modules folder is created. This folder holds the actual source code of the libraries you download.
node_modules folder can become very large. Never upload this folder to GitHub or share it directly. Instead, share your package.json file. When another developer gets your project, they just run npm install to download everything they need automatically.
- Global Installation: Use this for tools you want to use in any folder on your computer (like CLI tools or development servers).
npm install -g nodemon
4. Use Installed Packages
Once a package is in your node_modules folder, you can "require" it in your JavaScript files to use its functions. Here is how you would use the lodash library we just installed:
// Import the lodash library
const _ = require('lodash');
const numbers = [10, 20, 30, 40];
// Use lodash's reverse method
console.log(_.reverse(numbers)); // Output: [40, 30, 20, 10]
5. Install Specific Versions
Sometimes a new update to a library might break your code. npm allows you to lock your project to a specific version that you know works.
- To install a specific version:
npm install [email protected]
- To check for updates and bring a package to the latest safe version:
npm update express
6. Uninstall a Package
Cleaning up your project is important for performance and security. If you no longer need a library, remove it properly so it's deleted from node_modules and removed from your package.json.
- Remove a local project package:
npm uninstall lodash
- Remove a global tool:
npm uninstall -g nodemon
7. npm Scripts
One of the most powerful features of npm is the scripts section of your package.json. This allows you to create shortcuts for common development tasks.
{
"name": "my-cool-app",
"version": "1.0.0",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js",
"test": "echo 'Error: no test specified' && exit 1"
}
}
- To run these scripts, use:
npm run start
npm run dev
npm start or npm test. For any custom names like "dev" or "build", you must use npm run [name].
8. Publishing a Package
If you've written a piece of code that you think others would find useful, you can share it with the world.
- Create an account at npmjs.com.
- Link your terminal to your account:
npm login
- In your project folder (ensure your
package.jsonhas a unique name), run:
npm publish
Summary
npm is the backbone of the Node.js ecosystem. It handles the heavy lifting of downloading code, managing versions, and running automation scripts. By mastering npm, you gain access to the collective knowledge of millions of developers, allowing you to build complex, professional applications faster and with more reliability.