> For the complete documentation index, see [llms.txt](https://www.ankitavirani.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ankitavirani.com/experience/packege-mangers/npm-node-packege-manager.md).

# NPM-Node Packege Manager

## **NPM (Node Package Manager)** <a href="#id-2502" id="id-2502"></a>

NPM (Node Package Manager) is considered to be the largest software registry in the world. It is free, open-source, installed with Node.js, contains packages written in JSON. The main purpose of NPM is to provide automated dependency and package management. Those who use npm say it helps to improve your experience and the overall efficiency of Node.js development by allowing you to install the exact modules you need. The advantages of NPM are:

* ease of use for developers
* local package installation which helps save on space
* helps reduce the development time

That’s pretty much it, it’s very simple and performs its main function – uploading, storing, sharing, reusing software packages.

### How to Install NPM

NPM should be automatically installed when you install Node.js. To check is you have Node.js installed, run this command in your terminal:

```
node -v
```

If you already have Node.js installed and want to verify whether you also have NPM, run the following command in your terminal:

```
npm -v
```

FYI, npm updates happen more frequently than Node.js, and there are many npm versions out there, so you might want to keep your npm up to date, and possibly even update it right after you installed Node.js. To do that, run the following command:

```
npm install npm@latest -g
```

It might also be a good idea to use a version manager with your Node.js package, e.g. nodist or NVM.

### 🏗️ Architecture and Concepts

NPM follows a client-server architecture:

* 📡 Client: The NPM CLI tool installed on developers' machines
* 🖥️ Server: The NPM registry, a large public database of JavaScript packages

Key concepts I'm well-versed in:

* 📁 package.json: The heart of any Node.js project, defining project metadata and dependencies
* 🔒 package-lock.json: Ensures consistent installs across environments
* 📦 node\_modules: Directory where packages are installed
* 🌳 Dependency tree: Hierarchical structure of project dependencies
* 🔄 Semantic Versioning (SemVer): Version number scheme for packages

### 🖼️ NPM Architecture Diagram

<figure><img src="/files/xFUNhSK9XFxXq4gxl28F" alt=""><figcaption></figcaption></figure>

### 🛠️ NPM Commands

I am proficient in using a wide range of NPM commands for various tasks:

#### 📥 Installation Commands

```bash
# Install all dependencies
npm install

# Install a specific package
npm install lodash

# Install as dev dependency
npm install --save-dev jest

# Install globally
npm install -g typescript

# Install specific version
npm install react@17.0.2
```

#### 📜 Script Commands

I can efficiently manage and use NPM scripts defined in package.json:

```json
{
  "scripts": {
    "start": "node server.js",
    "dev": "next dev",
    "test": "jest",
    "build": "tsc",
    "lint": "eslint .",
    "deploy": "node deploy.js"
  }
}
```

Running these scripts:

```bash
npm run start
npm run dev
npm test
npm run build
npm run lint
npm run deploy
```

#### 🚀 Other Useful Commands

```bash
# Initialize a new project
npm init -y

# Update packages
npm update

# Remove a package
npm uninstall moment

# List installed packages
npm list

# Run security audit
npm audit

# Publish a package
npm publish
```

### 💻 Code Snippets

I can effectively use NPM packages in JavaScript/TypeScript projects. Here are some examples:

#### 🧩 Using a third-party package (lodash)

```tsx
import _ from 'lodash';

const numbers = [1, 2, 3, 4, 5];
const sum = _.sum(numbers);
console.log(`Sum: ${sum}`);

const users = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 28 },
  { name: 'Bob', age: 35 }
];
const sortedUsers = _.sortBy(users, 'age');
console.log('Sorted users:', sortedUsers);
```

#### 🛠️ Creating and publishing an NPM package

1. Create a new directory and initialize:

```bash
mkdir my-awesome-package
cd my-awesome-package
npm init -y
```

1. Create your main file (index.js):

```jsx
// index.js
function greet(name) {
  return `Hello, ${name}!`;
}

module.exports = { greet };
```

1. Update package.json:

```json
{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \\"Error: no test specified\\" && exit 1"
  },
  "keywords": ["greeting", "npm", "package"],
  "author": "Your Name",
  "license": "MIT"
}
```

1. Publish the package:

```bash
npm login
npm publish
```

### 🎓 Advanced NPM Skills

* 🔍 Troubleshooting dependency conflicts
* 🔒 Managing package security with npm audit
* 🚀 Optimizing install times with npm ci
* 📊 Analyzing package sizes with npm package-size
* 🔧 Configuring NPM for different environments

My comprehensive understanding of NPM enables me to efficiently manage dependencies, optimize project structures, and streamline development workflows in Node.js environments.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.ankitavirani.com/experience/packege-mangers/npm-node-packege-manager.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
