package.json vs Lockfile: Understanding Dependency Management in React

⚛️ Top 150 React Interview Questions – 90/150 📌 Topic: package.json vs. Lockfile ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHAT is it? 📄 package.json A manifest file that defines: • project metadata (name, scripts) • dependency version ranges (example: ^18.2.0) 🔒 Lockfile (package-lock.json / yarn.lock) Records the exact version of every dependency and all sub-dependencies installed at a specific time. ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHY use both? 📄 package.json Tells others what the project needs to run and allows flexible version updates using SemVer 🔒 Lockfile Ensures deterministic installs Everyone (devs + production) gets the exact same code Prevents “it works on my machine” bugs ━━━━━━━━━━━━━━━━━━━━━━ 🔹 HOW do they work together? ✏️ Edit package.json When adding dependencies or updating scripts ⚙️ Lockfile is auto-generated Created automatically when you run: npm install or yarn It locks versions and builds the dependency tree. ━━━━━━━━━━━━━━━━━━━━━━ 🔹 WHERE / Best Practices ✔ Commit Both Files Always push package.json and the lockfile to Git ❌ Never Edit Lockfile Manually If it breaks, delete it and run npm install again ✔ Understand Versioning package.json uses ranges (^, ~) Lockfile ignores ranges and pins one exact version ━━━━━━━━━━━━━━━━━━━━━━ 📝 SUMMARY (Easy to Remember) Think of a Grocery List vs. a Receipt 🛒 📄 package.json → Grocery List “Buy any brand of milk” 🧾 Lockfile → Receipt “Bought 1L Amul Milk at 10:05 AM for ₹60” The receipt proves exactly what you got. ━━━━━━━━━━━━━━━━━━━━━━ 👇 Comment “React” if this handbook is helping you 🔁 Share with someone preparing for React interviews #ReactJS #ReactInterview #packagejson #Lockfile #npm #Yarn #Top150ReactQuestions #LearningInPublic #Developers ━━━━━━━━━━━━━━━━━━━━━━

  • text, letter

To view or add a comment, sign in

Explore content categories