Node.js NPM (Node Package Manager)

Subject: Node.js

<h2>Node.js NPM (Node Package Manager)</h2><p>NPM (Node Package Manager) is the default package manager that comes with Node.js. It allows developers to install, share, and manage packages (modules or libraries) for Node.js projects. With NPM, integrating external libraries and tools into your project becomes seamless.</p><h3>What Is a Package?</h3><p>A package is a folder that typically includes:</p><ul><li>JavaScript code</li><li>A <code>package.json</code> file that contains metadata like name, version, dependencies</li></ul><h4>Popular NPM Packages</h4><ul><li><code>express</code> – Web framework</li><li><code>lodash</code> – Utility functions</li><li><code>mongoose</code> – MongoDB ODM</li><li><code>nodemon</code> – Automatically restarts server in development</li></ul><h3>Why Use NPM?</h3><ul><li>Install and manage third-party libraries</li><li>Track versions and dependencies easily</li><li>Simplify project configuration using <code>package.json</code></li><li>Reuse code from the open-source ecosystem</li></ul><h3>NPM Comes with Node.js</h3><p>When you install Node.js, NPM is included by default.</p><pre><code>node -v npm -v</code></pre><h3>Initializing a Project with <code>package.json</code></h3><pre><code>npm init</code></pre><pre><code>npm init -y</code></pre><h3>Installing Packages</h3><h4>1. As a Project Dependency (default)</h4><pre><code>npm install express</code></pre><h4>2. As a Development Dependency</h4><pre><code>npm install nodemon --save-dev</code></pre><h3>Global vs Local Installation</h3><table><tr><th>Type</th><th>Description</th><th>Command</th></tr><tr><td>Local</td><td>Installs package in project folder</td><td><code>npm install express</code></td></tr><tr><td>Global</td><td>Installs package system-wide</td><td><code>npm install -g nodemon</code></td></tr></table><h3>Using Installed Packages</h3><pre><code>const express = require('express'); const app = express();

app.listen(3000, () => { console.log('Server is running'); });</code></pre><h3>Example <code>package.json</code></h3><pre><code>{ "name": "myapp", "version": "1.0.0", "description": "My first app", "main": "index.js", "scripts": { "start": "node index.js", "dev": "nodemon index.js" }, "dependencies": { "express": "^4.18.2" }, "devDependencies": { "nodemon": "^3.0.1" } }</code></pre><h3>Common NPM Commands</h3><table><tr><th>Command</th><th>Description</th></tr><tr><td><code>npm init</code></td><td>Create <code>package.json</code> interactively</td></tr><tr><td><code>npm init -y</code></td><td>Create with default values</td></tr><tr><td><code>npm install <pkg></code></td><td>Install a package locally</td></tr><tr><td><code>npm install -g <pkg></code></td><td>Install a package globally</td></tr><tr><td><code>npm uninstall <pkg></code></td><td>Remove a package</td></tr><tr><td><code>npm update</code></td><td>Update all dependencies</td></tr><tr><td><code>npm list</code></td><td>Show installed packages</td></tr><tr><td><code>npm outdated</code></td><td>Show outdated packages</td></tr></table><h3>What is <code>package-lock.json</code>?</h3><ul><li>Automatically created when you run <code>npm install</code></li><li>Records the exact version of installed dependencies</li><li>Ensures consistent installs across environments</li></ul><h3>Publishing Your Own Package</h3><ol><li>Create an account at <a href="https://www.npmjs.com">npmjs.com</a></li><li>Login via terminal:</li><pre><code>npm login</code></pre><li>Publish your module:</li><pre><code>npm publish</code></pre></ol><h3>Key Takeaways</h3><ul><li>NPM is included with Node.js for managing packages.</li><li>Use <code>package.json</code> to track dependencies.</li><li>Install packages locally (project-specific) or globally (system-wide).</li><li><code>package-lock.json</code> ensures consistency in installations.</li><li>NPM allows you to both use and publish JavaScript libraries efficiently.</li></ul>

Next : Node.js ES Modules