NPM stands for Node Package Manager. It is the default package manager for the Node.js runtime environment. NPM helps developers manage packages (libraries, tools, and reusable code modules) that extend or add functionality to a Node.js project. It provides two main components:
NPM has revolutionized the way JavaScript applications are developed. Hereβs why:
NPM is bundled with Node.js. To install NPM, you simply install Node.js from its official site:
https://nodejs.org
After installation, verify the installation:
node -v
npm -v
This command initializes a new Node.js project by creating a package.json file.
npm init
It prompts the user for details such as project name, version, description, entry point, author, and license. To skip prompts, use:
npm init -y
This command installs a package from the NPM registry into your project.
npm install express
It installs the package in the node_modules directory and updates the package.json and package-lock.json files.
npm install nodemon --save-dev
The --save-dev flag installs packages that are needed only for development and not for production.
Global packages are installed system-wide and can be used in terminal/CLI globally.
npm install -g nodemon
Removes a package from the project.
npm uninstall express
Updates all installed packages to the latest version allowed by the version ranges in package.json.
npm update
Shows all installed packages and their dependencies.
npm list
To see globally installed packages:
npm list -g --depth=0
The package.json file is the heart of any Node.js project. It contains metadata about the project and its dependencies.
{
"name": "my-app",
"version": "1.0.0",
"description": "My awesome Node app",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^2.0.22"
},
"author": "John Doe",
"license": "ISC"
}
This file locks the installed dependency versions to ensure consistent installs across different environments.
It provides:
NPM uses semantic versioning (semver). A version number looks like: 1.2.3
"express": "^4.18.2" // Can update to 4.x.x but not 5.x.x
"express": "~4.18.2" // Can update to 4.18.x only
"express": "4.18.2" // Locked to this exact version
Inside the scripts section of package.json, you can define command aliases.
"scripts": {
"start": "node app.js",
"test": "echo 'Running Tests'",
"dev": "nodemon app.js"
}
Run a script using:
npm run dev
You can also omit run for predefined scripts like start or test:
npm start
npm test
mkdir my-npm-package
cd my-npm-package
npm init
// index.js
function greet(name) {
return `Hello, ${name}!`;
}
module.exports = greet;
# my-npm-package
This package greets users by name.
npm login
npm publish
After publishing, your package is available at:
https://www.npmjs.com/package/your-package-name
npm install githubuser/repo-name
npm install git+https://github.com/user/repo.git
npm install ./local-folder
npm audit
npm audit fix
Sometimes, you need to manually upgrade or modify packages.
npm install package@latest
NPM stores downloaded packages in a cache folder. You can clean it with:
npm cache clean --force
NPM is a cornerstone of modern JavaScript and Node.js development. It not only allows access to a vast ecosystem of packages but also simplifies project setup, dependency management, and deployment. Whether you're working on a small tool or a large application, understanding and leveraging NPM effectively is crucial for productivity and consistency.
From installing and managing packages to creating your own modules and publishing them to the registry, NPM empowers developers to build modular, maintainable, and scalable applications with ease. Its CLI tools, configuration options, and vast ecosystem make it an indispensable part of any Node.js development workflow.
A function passed as an argument and executed later.
Runs multiple instances to utilize multi-core systems.
Reusable blocks of code, exported and imported using require() or import.
nextTick() executes before setImmediate() in the event loop.
Starts a server and listens on specified port.
Node Package Manager β installs, manages, and shares JavaScript packages.
A minimal and flexible web application framework for Node.js.
A stream handles reading or writing data continuously.
It processes asynchronous callbacks and non-blocking I/O operations efficiently.
Node.js is a JavaScript runtime built on Chrome's V8 engine for server-side scripting.
An object representing the eventual completion or failure of an asynchronous operation.
require is CommonJS; import is ES6 syntax (requires transpilation or newer versions).
Use module.exports or exports.functionName.
Variables stored outside the code for configuration, accessed using process.env.
MongoDB, often used with Mongoose for schema management.
Describes project details and manages dependencies and scripts.
Synchronous blocks execution; asynchronous runs in background without blocking.
Allows or restricts resources shared between different origins.
Use try-catch, error events, or middleware for error handling.
Provides file system-related operations like read, write, delete.
Using event-driven architecture and non-blocking I/O.
Functions in Express that execute during request-response cycle.
A set of routes or endpoints to interact with server logic or databases.
Yes, it's single-threaded but handles concurrency using the event loop and asynchronous callbacks.
Middleware to parse incoming request bodies, like JSON or form data.
Copyrights © 2024 letsupdateskills All rights reserved