Packages and package.json
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.jsonfile 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 expressNow 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 testCreating package.json manually
- Automatically (asks questions in the console):
javascript
npm init- Quickly (with defaults, no questions):
javascript
npm init -yHow 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 |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.