
Understanding package.json in a JavaScript or TypeScript App
Learn what package.json does, how it defines your JS/TS project’s dependencies, scripts, metadata, and configuration for Node.js, Bun, npm, and TypeScript.
dogunfx
Every JavaScript or TypeScript project has a file called package.json at its root.
It’s the heart of your project configuration — describing everything from your app’s name and version to its dependencies, scripts, and build settings.
In this post, we’ll break down package.json line by line — what it is, why it matters, and how you can use it effectively across Node.js, Bun, and TypeScript projects.
1) What is package.json?
It’s a simple JSON file that defines your project’s metadata, scripts, and dependencies.
{
"name": "my-awesome-app",
"version": "1.0.0",
"description": "A cool project using JavaScript and TypeScript",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node src/index.js",
"dev": "bun run dev",
"build": "tsc"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"typescript": "^5.3.0"
}
}
It tells Node or Bun how to run, build, and understand your app.
2) Key Sections Explained
🏷️ name and version
Every package needs a name and version (especially if published to npm).
"name": "my-awesome-app",
"version": "1.0.0"
Use lowercase and hyphens for the name.
Versions follow Semantic Versioning (SemVer) — MAJOR.MINOR.PATCH.
📦 dependencies and devDependencies
These define external libraries your app uses.
"dependencies": {
"express": "^4.18.2",
"axios": "^1.6.0"
},
"devDependencies": {
"typescript": "^5.3.0",
"@types/node": "^20.0.0"
}
dependencies→ needed in production.devDependencies→ used only during development (e.g. TypeScript, linters, test libs).
💡 In Bun or npm, install with
--save-devfor dev dependencies:npm install typescript --save-devorbun add -d typescript
⚙️ scripts — Your Project Commands
Define short aliases for CLI commands.
"scripts": {
"start": "node src/server.js",
"dev": "nodemon src/server.js",
"build": "tsc",
"test": "vitest"
}
Run with:
npm run dev
# or
bun run dev
Special case: "start" can be run directly with npm start or bun start.
🔄 type — Module System (CommonJS vs ESM)
Determines how Node interprets files (require vs import).
"type": "module"
Options:
| Value | Meaning |
|---|---|
"module" | Use ES Modules (import/export) |
"commonjs" | Use CommonJS (require/module.exports) |
If not set, Node assumes "commonjs".
This setting affects .js files, not .ts (TS compiles based on tsconfig.json).
🚀 main, module, and types
These tell consumers which file to load.
| Field | Purpose | Example |
|---|---|---|
main | CommonJS entry | "main": "dist/index.js" |
module | ESM entry | "module": "dist/index.mjs" |
types | TypeScript types | "types": "dist/index.d.ts" |
Example for a TypeScript library:
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts"
🧩 engines
Specifies the runtime versions supported by your app.
"engines": {
"node": ">=18",
"bun": ">=1.0.0"
}
🧰 scripts + Tool Integration
Many tools (like TypeScript, ESLint, Vite, Next.js) read configuration from package.json.
Example — Vite project:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"vite": {
"port": 5173
}
}
3) Using package.json in TypeScript Projects
When using TypeScript, package.json works hand-in-hand with tsconfig.json.
Example folder structure:
my-ts-app/
├── package.json
├── tsconfig.json
├── src/
│ └── index.ts
└── dist/
└── index.js
scripts section:
"scripts": {
"build": "tsc",
"start": "node dist/index.js"
}
After running npm run build, TypeScript compiles .ts → .js.
4) Managing Packages (npm, pnpm, Bun)
npm
npm install express
npm run dev
pnpm (faster, space-efficient)
pnpm add express
pnpm dev
Bun (fast runtime + package manager)
bun add express
bun run dev
All of these update your package.json automatically and lock dependencies in a package-lock.json, pnpm-lock.yaml, or bun.lockb file.
5) Optional Fields Worth Knowing
| Field | Description | Example |
|---|---|---|
license | Declares project license | "MIT" |
author | Your name or org | "author": "Dogunfx <me@example.com>" |
repository | Git repo info | "repository": "github:dogunfx/my-app" |
keywords | Helps discover on npm | ["node", "typescript", "api"] |
private | Prevent accidental publishing | "private": true |
bin | Exposes CLI command | "bin": { "mycli": "./cli.js" } |
6) Advanced: exports Field
Used in modern packages to control module entry points (ESM + CJS).
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
This ensures compatibility across environments (Node, bundlers, Bun).
7) Validating and Formatting
To validate your file:
npm pkg check
To inspect a value:
npm pkg get name
You can also format automatically using:
npm pkg fix
8) Real Example: Next.js or Bun Project
Next.js
{
"name": "my-next-app",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "14.0.0",
"react": "18.3.0",
"react-dom": "18.3.0"
}
}
Bun
{
"name": "bun-server",
"type": "module",
"scripts": {
"dev": "bun run src/server.ts"
},
"dependencies": {
"hono": "^3.7.0"
}
}
9) Summary
| Key Field | Purpose |
|---|---|
name, version | Identify the package |
dependencies | Production libraries |
devDependencies | Dev-only tools |
scripts | CLI commands |
type | Module system (CJS/ESM) |
main / module / types | Entry points |
engines | Runtime requirements |
private | Prevent publishing |
10) References
- npm Docs — package.json
- MDN — Package management basics
- TypeScript Docs — Module Resolution
- Bun Docs — Package Manager
package.json is more than just a dependency list — it’s the blueprint of your project’s lifecycle.
Understanding it deeply helps you automate builds, streamline workflows, and manage your JavaScript or TypeScript apps like a pro.
Next.js vs Nuxt: The Battle of the Meta Frameworks
A deep comparison between Next.js and Nuxt—the two meta frameworks powering React and Vue ecosystems. We explore their philosophies, performance, ecosystems, and what makes each one a cornerstone of modern web development.
Programming in the Small 3: Objects, JSON, and APIs in JavaScript
A practical guide to JavaScript objects, working with JSON, and calling APIs (Node.js/Bun) — how data flows between frontend, backend, and a database.