Node.js package.json
Node.js – package.json
package.json is the heart of any Node.js project.
It stores important information about your project such as:
Project name
Version
Dependencies
Scripts
Metadata (author, license, etc.)
Configurations for npm and modules
It helps Node.js understand how to run, build, and manage your application.
✅ 1. How to Create package.json
Method 1: Auto-generate (recommended)
Use npm to create it:
NPM will ask you several questions.
Method 2: Fast auto-generate
This creates a default package.json file instantly.
✅ 2. Example of a Basic package.json File
✅ 3. Important Fields in package.json
1. name
The project name (must be lowercase, no spaces).
2. version
Current version (semantic versioning: major.minor.patch).
3. description
Short explanation of your project.
4. main
Entry point file (default: index.js).
5. scripts
Used to run commands.
Example:
Run:
or
6. dependencies
Libraries needed in production.
Example:
It adds to:
7. devDependencies
Libraries used only in development (not needed in production).
Example:
Adds to:
8. license
Specifies the software license (MIT, ISC, etc.).
✅ 4. package-lock.json
When you install a package, NPM creates:
It locks the exact versions of dependencies to ensure consistency across environments.
✅ 5. node_modules Folder
This contains all installed packages and libraries.
Never upload node_modules to GitHub.
Instead, use:
to recreate it from package.json.
✅ 6. Why package.json is Important?
| Feature | Purpose |
|---|---|
| Dependency Management | Install/update packages easily |
| Scripts | Run development or production commands |
| Project Metadata | Share project information |
| Version Control | Manage software versions |
| Reproducibility | Helps recreate environment |
