# NPM

## What is NPM ?

NPM(Node package Manager) is the package manager that allows developer to install, manage and share reusable packages.

NPM is the default package manager commonly used in Node.js. It allows developers to install third-party packages, manage project dependencies, and define scripts through the `package.json` file. It also provides access to the npm registry, where developers can publish and download packages.

Suppose you're building a backend using Node.js and need Express to handle HTTP requests.

Instead of writing all the required functionality yourself, you can install Express using:

```shell
npm install express
```

NPM downloads the package and its required dependencies, places them in `node_modules`, and records the dependency in `package.json` by default.

Important terms:

| Term | Meaning |
| --- | --- |
| npm CLI | The command-line tool used to manage packages and scripts. |
| npm Registry | An online repository where JavaScript packages are published and distributed. |
| `package.json` | Stores project metadata, dependencies, and scripts. |
| `node_modules` | Directory where installed packages are typically stored. |
| `package-lock.json` | Records the resolved dependency tree and versions to support reproducible installs. |

Node.js is a JavaScript runtime, whereas npm is a package manager. They are different tools, even though npm is commonly installed alongside Node.js.

Also, frameworks such as Express are packages distributed through npm; npm itself doesn't build or manage their internal code.

### Difference Between `package.json` and `package-lock.json`

| Feature | package.json | package-lock.json |
| --- | --- | --- |
| Main purpose | Defines project requirements | Locks resolved dependency versions |
| Dependency versions | Usually ranges | Exact resolved versions |
| Transitive dependencies | Generally not listed individually | Recorded |
| Scripts | Yes | No |
| Manually edited | Commonly | Generally avoided |
| Automatically updated by npm | Yes, for relevant commands | Yes |
| Commit to Git | Yes | Yes, for applications |
