Back to Tutorials
Tools & Productivity
15 min read
Sahasransu Satpathy
9/1/2025

NPM & Package Management

Learn how to manage JavaScript packages effectively using NPM, including installation, updates, scripts, and best practices.

<h2>Introduction</h2>
<p>NPM (Node Package Manager) is the most widely used package manager for JavaScript projects. It allows developers to install, update, and manage dependencies for both frontend and backend projects.</p>

<h3>1. Installing NPM & Node.js</h3>
<pre><code>{`

Check if Node.js and npm are installed

node -v npm -v

If not installed, download from https://nodejs.org/

`}</code></pre>

<h3>2. Initializing a Project</h3>
<pre><code>{`

Create a new package.json file

npm init

OR for default options

npm init -y `}</code></pre>

<h3>3. Installing Packages</h3>
<ul>
  <li>Install a package locally: <code>npm install package-name</code></li>
  <li>Install a package globally: <code>npm install -g package-name</code></li>
  <li>Install a specific version: <code>npm install package-name@1.2.3</code></li>
  <li>Save as dev dependency: <code>npm install package-name --save-dev</code></li>
</ul>

<h3>4. Updating & Removing Packages</h3>
<ul>
  <li>Update a package: <code>npm update package-name</code></li>
  <li>Uninstall a package: <code>npm uninstall package-name</code></li>
</ul>

<h3>5. Using NPM Scripts</h3>
<p>You can define scripts in <code>package.json</code>:</p>
<pre><code>{`

{ "scripts": { "start": "node server.js", "build": "webpack --config webpack.config.js", "test": "jest" } } `}</code></pre> <p>Run scripts using: <code>npm run start</code>, <code>npm run build</code></p>

<h3>6. Best Practices</h3>
<ul>
  <li>Use semantic versioning for dependencies</li>
  <li>Keep <code>package-lock.json</code> under version control</li>
  <li>Regularly update dependencies to avoid security vulnerabilities</li>
  <li>Use <code>npx</code> for running one-time commands without installing globally</li>
  <li>Organize dev dependencies separately from production dependencies</li>
</ul>

<h3>7. Extras</h3>
<ul>
  <li>Code snippets included for common npm commands</li>
  <li>Tips for efficient package management in large projects</li>
  <li>Best practices for team collaboration using npm</li>
</ul>

<h3>Conclusion</h3>
<p>By mastering NPM and package management, you can maintain clean, efficient, and secure JavaScript projects while improving productivity.</p>

Previous Tutorial

Browse All Tutorials