Node.js NPM (Node Package Manager)
Subject: Node.js
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>