Skip to main content

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.json file that can be installed and used as a dependency.

NamePurpose
expressBuilding web servers and APIs
dotenvWorking with .env files (environment variables)
mongooseWorking with MongoDB
lodashUtilities for arrays and objects
axiosHTTP requests (fetch for Node.js)
chalkColoring console output
jestTesting

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

FieldDescription
nameThe package or project name (unique in npm if published)
versionThe project's version (semantic versioning)
descriptionA short project description
mainThe entry point: the file that runs when imported (require('my-package'))
scriptsCommands you can run via npm run
dependenciesPackages the project needs in production
devDependenciesPackages needed only for development (tests, build, etc.)
authorThe project's author
licenseThe 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
  1. Quickly (with defaults, no questions):
javascript
npm init -y

How packages and package.json relate

ElementRole
PackageThe library or project code
package.jsonDescribes the package: what it is, its dependencies, how to run it
npm / pnpm / yarnThe package manager that reads package.json and installs the needed packages

Summary

ConceptDescription
PackageA reusable Node.js library or module
package.jsonThe file describing a project and its dependencies
npmThe tool for managing packages
node_modulesThe folder where installed packages live

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.