NPM (Node Package Manager) is a powerful package manager for JavaScript. It comes bundled with Node.js and is the default package manager used to install, update, and manage Node.js libraries. It allows developers to easily include and manage open-source libraries and tools in their projects, improving productivity and software development efficiency.
NPM consists of:
With NPM, developers can:
npm -v
This command returns the installed version of NPM. Node.js must be installed for NPM to work.
Before installing packages, initialize your project with a package.json file. This file holds metadata about your project and the list of dependencies.
npm init
This command starts an interactive CLI to create the package.json file. For quick setup:
npm init -y
This creates a default package.json file without prompts.
Local packages are installed in the current project directory inside the node_modules folder. These packages are listed in package.json under dependencies or devDependencies.
npm install package-name
Example:
npm install express
This installs the Express web framework.
Global packages are available system-wide and are usually used for command-line tools.
npm install -g package-name
Example:
npm install -g nodemon
Global packages are installed in a directory accessible from your system PATH.
You can install a particular version of a package by specifying the version number:
npm install package-name@version
Example:
npm install express@4.17.1
Packages used only in development (e.g., testing tools, linters) should be installed as dev dependencies.
npm install package-name --save-dev
Example:
npm install mocha --save-dev
The package.json file defines the projectβs metadata and dependencies. Example:
{
"name": "my-app",
"version": "1.0.0",
"description": "Sample Node.js App",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "mocha"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"mocha": "^10.0.0"
}
}
If you clone or copy a project that already contains a package.json file, install all its dependencies using:
npm install
This command reads the package.json and installs all listed dependencies.
When installing packages, NPM creates a package-lock.json file to lock the version of each dependency (including nested ones). It ensures consistent installation across different environments.
This file should always be committed to version control for consistent builds.
To update an existing package to the latest version compatible with the version range in package.json:
npm update
To update to the latest version (ignoring specified version ranges):
npm install package-name@latest
To remove a package from your project:
npm uninstall package-name
This removes the package from node_modules and from package.json.
To list local packages:
npm list
To list global packages:
npm list -g --depth=0
NPM uses Semantic Versioning for package versions, usually in the format MAJOR.MINOR.PATCH.
Version ranges in package.json:
You can define and run scripts in the scripts section of package.json.
Example:{
"scripts": {
"start": "node index.js",
"test": "mocha test.js"
}
}
Run using:
npm run start
npm run test
npx is a tool that comes with NPM to run packages without installing them globally.
npx create-react-app my-app
This runs the create-react-app package directly.
mkdir my-package
cd my-package
npm init
// index.js
module.exports = function(name) {
return `Hello, ${name}`;
};
Include a proper name, version, description, and entry point in package.json.
First, login:
npm login
Then publish:
npm publish
NPM caches downloaded packages to speed up future installations. To view the cache location:
npm config get cache
To clean the cache:
npm cache clean --force
You can configure various settings using:
npm config set key value
npm config get key
For example:
npm config set registry https://registry.npmjs.org/
npm audit
npm audit fix
Occurs when installing global packages. Fix with:
sudo chown -R $USER /usr/local/lib/node_modules
Reinstall node_modules:
rm -rf node_modules
npm install
npm cache clean --force
While NPM is widely used, alternatives like Yarn and PNPM have gained popularity.
Mastering NPM is essential for modern JavaScript and Node.js development. It simplifies dependency management, allows the use of open-source tools, and improves productivity. Whether you're building a small script or a large-scale enterprise app, understanding how to install, update, and manage packages using NPM helps ensure that your project remains maintainable, secure, and efficient.
By using commands like npm install, npm update, and npm audit, developers can maintain high standards in their project workflows. Moreover, the ability to create and share packages fosters a strong developer ecosystem. As the JavaScript community evolves, NPM will continue to be a central part of project and package management.
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