🚫 Stop creating instances manually in your Node.js apps. If you're doing this in your code: 👉 const userService = new UserService(); You’re quietly building a trap for your future self. Here’s why. 💥 The Problem: The "Hardcoded" Nightmare Imagine your app has 50 controllers all using new UserService(). Tomorrow, your PM says: "The DB is slow, we need to swap this for a CachedUserService." Without Dependency Injection (DI), you now have to: Open 50 files. Manually change the import and class name in every single one. Hope you didn't break a test. There goes your weekend. --- ✅ The Solution: How NestJS saves you Instead of creating the instance yourself, you just "ask" for it in the constructor: 👉 constructor(private readonly userService: UserService) {} What’s happening behind the scenes? NestJS uses DI to act as a "middleman." You define the wiring in ONE central place (the Module): { provide: UserService, useClass: CachedUserService } Why this is a game-changer: 1️⃣ The 5-Second Swap: Need to change logic? Update one line in the Module. The 50 controllers using it don't need a single change. 2️⃣ Seamless Testing: Want to mock the service for a unit test? Just "inject" a mock version. 3️⃣ Decoupled Code: Your controllers don't care how a service works; they just know it has the methods they need. 📦 Real-world impact: In large systems, DI is the difference between a maintainable architecture and a spaghetti-code disaster. 🎯 One takeaway: If your classes are creating their own dependencies, you're limiting your system’s scalability. Let the framework do the heavy lifting. #NodeJS #NestJS #SoftwareEngineering #CleanCode #WebDevelopment #Backend
I appreciate the clear breakdown Ashraf Harba, the before/after alone is worth saving Once your codebase thinks in abstractions you can swap persistence layers run different implementations per environment or lazily load expensive services without touching business logic. It stops being a framework feature and becomes an architectural mindset The practical examples you shared are great for getting started and once that mindset clicks it opens up even more than just the refactoring wins
People still manually inject instances in 2026?🫠 I think instead of Nest, inversify for DI and express works for low-medium projects. I have worked with both. Great read