---
id: 69a99035edde234d544d003f
title: NPM Review
challengeType: 31
dashedName: review-npm
---

# --description--

## What is npm?

- **Definition**: npm is an online registry for open-source software, functioning like an "app store" for code.
- **Official Meaning**: npm does not officially stand for "Node Package Manager"; it is a recursive backronymic abbreviation for "npm is not an acronym".
- **Three Related Parts**:

  - **Website (npmjs.com)**: For looking up packages in the registry.
  - **Registry**: A large public database storing all packages.
  - **CLI (Command Line Interface)**: A tool installed with Node.js to manage project dependencies.

- **Alternatives**: While npm is most widely used, popular alternatives include **Yarn**, **PNPM**, and **Bun**.

## The package.json File

- **Purpose**: A configuration file that stores metadata, dependencies, and scripts for a Node.js project.
- **Key Fields**:

  - `name` and `version`: Identify the project.
  - `main`: Specifies the entry point file, often `index.js`.
  - `license`: Defines legal terms (default is `ISC`).
  - `dependencies`: Packages required to run the app in production.
  - `devDependencies`: Tools only needed during development, such as testing frameworks.


- **Creation**:

  - `npm init`: Starts an interactive setup process.
  - `npm init -y`: Installs a `package.json` with default values instantly.

## Semantic Versioning (SemVer)

- **Format**: `MAJOR.MINOR.PATCH`.
- **Rules**:

  - **MAJOR**: Breaking changes that can affect existing code.
  - **MINOR**: New features that are backwards compatible.
  - **PATCH**: Small bug fixes and improvements.

- **Range Symbols**:

  - `^` (Caret): Allows minor and patch updates (e.g., `^1.2.3` allows `1.x.x`).
  - `~` (Tilde): Allows only patch updates (e.g., `~1.2.3` allows `1.2.x`).
  - `*` (Asterisk): Matches any version; used primarily for testing.

## Dependency Management

- **Installation**:

  - `npm install <name>`: Adds a production dependency.
  - `npm install <name> -D`: Adds a development dependency.
  - `npm install <name>@<version>`: Installs a specific version.

- **Node Modules & Locks**:

  - `node_modules/`: Contains the actual code for installed packages.
  - `package-lock.json`: Locks the exact versions of all dependencies and "child dependencies" to ensure consistency across environments.

- **Removal**: `npm uninstall <name>` removes the package and updates `package.json`.
- **Maintenance**:

  - `npm outdated`: Identifies which packages have newer versions available.
  - `npm update`: Updates packages within the allowed SemVer range.

## npm Scripts

- **Function**: Custom commands defined in the `scripts` section of `package.json` to automate tasks.
- **Execution**: Run via `npm run <script_name>`.
- Shorthands exist for `start`, `test`, `stop`, and `restart` (e.g., `npm start`).
- **Sequential Execution**:
- `&&`: Runs the second command only if the first succeeds.
- `;`: Runs commands one after another regardless of success.
- **Concurrent Execution**: Use the `&` operator to run processes in the background.
- **Arguments**: Pass arguments to scripts using the `--` separator (e.g., `npm start -- 8000`).

## Publishing to the npm Registry

- **Registry Access**: A database of public and private packages.
- **Publication Steps**:

1. Create a free account at `npmjs.com`.
2. Log in via terminal using `npm login`.
3. Ensure a unique `name` and valid `version` are in `package.json`.
4. Create a `README.md` and `.npmignore` (to exclude sensitive files).
5. Run `npm publish` (or `npm publish --access public` for scoped packages).

- **Scopes**: Used to avoid name conflicts by publishing under `@username/package-name`.

## Module Systems in Node.js

### CommonJS (CJS)

- **Default System**: The original way Node.js handles modules; loads synchronously.
- **Syntax**:
- **Import**: `const module = require('./path')`.
- **Export**: `module.exports = { functionName }`.
- **Identification**: Used for `.cjs` files or `.js` files when no `type` is specified in `package.json`.

### ES Modules (ESM)

- **Standardized Format**: Modern JavaScript standard; loads asynchronously for efficiency.
- **Syntax**:

  - **Export**: `export { name }` or `export default function`.
  - **Import**: `import { name } from './path.mjs'`.
  - **Identification**: Used for `.mjs` files or `.js` files when `package.json` contains `"type": "module"`.

# --assignment--
   
Review the npm topics and concepts.
