Suggest an editImprove this articleRefine the answer for “Packages and package.json”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)A **package** is a set of reusable JavaScript code you can install via npm, and **`package.json`** is a project's main configuration file, describing its dependencies, scripts and metadata. **Key point:** `package.json` tells npm/pnpm/yarn which packages to install into `node_modules`.Shown above the full answer for quick recall.Answer (EN)Image## What a package is in Node.js A **package** is simply a **set of JavaScript code** you can reuse. It's usually a **library, module or utility** that can be installed via npm (Node Package Manager). ### In other words: > **A package = a project with a** `package.json` **file that can be installed and used as a dependency.** ### Examples of popular packages: | Name | Purpose | |---|---| | `express` | Building web servers and APIs | | `dotenv` | Working with `.env` files (environment variables) | | `mongoose` | Working with MongoDB | | `lodash` | Utilities for arrays and objects | | `axios` | HTTP requests (fetch for Node.js) | | `chalk` | Coloring console output | | `jest` | Testing | ### Where packages are stored Once installed via npm/pnpm/yarn, packages land in the folder: ```javascript node_modules/ ``` Their versions and the list of them are stored in `package.json`. ### Installing a package ```javascript npm install express ``` Now you can use it in code: ```javascript const express = require('express'); const app = express(); app.get('/', (req, res) => res.send('Hello from Express!')); app.listen(3000); ``` ## What `package.json` is `package.json` is a Node.js project's **main configuration file**. It describes: - the project, - its dependencies, - scripts, - metadata (name, version, license, etc.). ### A simple `package.json` example ```javascript { "name": "my-node-app", "version": "1.0.0", "description": "Example Node.js project", "main": "index.js", "scripts": { "start": "node index.js", "dev": "nodemon index.js" }, "dependencies": { "express": "^4.18.2" }, "devDependencies": { "nodemon": "^3.0.1" }, "author": "Tim", "license": "MIT" } ``` ## Main fields of `package.json` | Field | Description | |---|---| | `name` | The package or project name (unique in npm if published) | | `version` | The project's version (semantic versioning) | | `description` | A short project description | | `main` | The entry point: the file that runs when imported (`require('my-package')`) | | `scripts` | Commands you can run via `npm run` | | `dependencies` | Packages the project needs in production | | `devDependencies` | Packages needed only for development (tests, build, etc.) | | `author` | The project's author | | `license` | The license type (for example, MIT, ISC, etc.) | ### Using scripts If `package.json` has: ```javascript "scripts": { "start": "node index.js", "test": "jest" } ``` You can run: ```javascript npm start npm run test ``` ## Creating `package.json` manually 1. Automatically (asks questions in the console): ```javascript npm init ``` 2. Quickly (with defaults, no questions): ```javascript npm init -y ``` ## How packages and `package.json` relate | Element | Role | |---|---| | **Package** | The library or project code | | `package.json` | Describes the package: what it is, its dependencies, how to run it | | **npm / pnpm / yarn** | The package manager that reads `package.json` and installs the needed packages | ## Summary | Concept | Description | |---|---| | **Package** | A reusable Node.js library or module | | `package.json` | The file describing a project and its dependencies | | **npm** | The tool for managing packages | | `node_modules` | The folder where installed packages live |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.