When starting a React project, you’ll encounter a file called package.json
. This file acts like a roadmap for your project, listing the tools, libraries, and scripts your app needs to work. Here’s a beginner-friendly breakdown of what each entry in your package.json
means.
Basic Information
{
"name": "learning",
"private": true,
"version": "0.0.0",
"type": "module"
}
name
: The name of your project (learning
).- Keep it simple and unique.
private
: Ensures your project isn’t accidentally published as a public npm package.true
means it’s safe from being shared unintentionally.
version
: Indicates the current version of your project.- Starts at
0.0.0
, and you update it as your project grows.
- Starts at
type
: Specifies the module system used."module"
enables ES modules (import/export
) instead of olderrequire
syntax.
Scripts
Scripts automate tasks like starting the development server or building your app.
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint src",
"preview": "vite preview",
"prepare": "husky install"
}
dev
: Starts your app in development mode usingvite
.- Run with:
npm run dev
.
- Run with:
build
: Creates a production-ready version of your app.- Run with:
npm run build
.
- Run with:
lint
: Checks your code for errors usingeslint
.- Run with:
npm run lint
.
- Run with:
preview
: Serves the built app locally for testing.- Run with:
npm run preview
.
- Run with:
prepare
: Sets uphusky
for Git hooks.- Runs automatically after
npm install
.
- Runs automatically after
Dependencies
Dependencies are the libraries your project needs to run.
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
react
: The core React library for building user interfaces.- Version
^18.2.0
means it’ll use18.2.x
and updates within this range.
- Version
react-dom
: Integrates React with the web by managing the DOM (HTML structure).
Dev Dependencies
Dev dependencies are tools needed during development, not when the app is live.
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"@vitejs/plugin-react": "^4.2.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-react": "^7.33.2",
"husky": "^8.0.3",
"lint-staged": "^15.1.0",
"prettier": "^3.1.0",
"vite": "^5.0.0"
}
Here’s what they do:
@commitlint/*
: Enforces consistent Git commit messages.@types/*
: Adds TypeScript type definitions forreact
andreact-dom
.@vitejs/plugin-react
: A plugin to make React work seamlessly with Vite.eslint
: A tool to catch code errors and maintain coding standards.eslint-config-prettier
: Prevents conflicts betweeneslint
andprettier
.eslint-plugin-jsx-a11y
: Ensures accessibility in your JSX (React code).eslint-plugin-react
: Adds React-specific linting rules.husky
: Automates Git hooks to ensure quality before commits.lint-staged
: Runs linting (and other tools) on staged files during a Git commit.prettier
: Formats your code to look consistent.vite
: A modern, fast build tool for development and production.
Lint-Staged
This section defines which files should be linted or formatted before being committed to Git.
"lint-staged": {
"**/*.{js,jsx}": [
"npx prettier --write",
"npx eslint --fix"
]
}
"**/*.{js,jsx}"
: Targets all JavaScript and JSX files in the project.npx prettier --write
: Formats code with Prettier.npx eslint --fix
: Automatically fixes linting errors.
How Everything Fits Together
- Start Development: Run
npm run dev
to launch your project. - Build for Production: Use
npm run build
to optimize your app. - Lint Your Code: Run
npm run lint
to clean up errors. - Git Automation: Git hooks (via
husky
andlint-staged
) ensure code quality during commits.
Why This Matters
These entries make your project:
- Organized: Clearly defines dependencies and tasks.
- Efficient: Automates repetitive tasks like formatting or building.
- Maintainable: Ensures your code meets standards and avoids errors.