πŸš€ Backend Internals #2: What Actually Happens When You Call `require()`?

μž‘μ„±μž

μΉ΄ν…Œκ³ λ¦¬:

← ν”Όλ“œλ‘œ
DEV Community · Krati Joshi · 2026-07-23 개발(SW)
Cover image for πŸš€ Backend Internals #2: What Actually Happens When You Call `require()`?

Krati Joshi

In my previous post, we learned that Node.js caches modules, so the same file isn’t executed twice.

But have you ever wondered what actually happens the first time you call require()?

Let’s break it down.

πŸ“¦ Imagine a Real-Life Scenario

Think of require() like ordering food at a restaurant.

You (your code) place an order (require()).

The waiter (Node.js) doesn’t cook the food. Instead, they:

  • 🍽️ Find the right chef (locate the module)
  • πŸ“‹ Check if the dish is already prepared (module cache)
  • πŸ‘¨β€πŸ³ Ask the chef to cook it if needed
  • πŸ› Serve the food (return module.exports)
  • 🧠 Remember it for the next customer (cache it)

That’s exactly how require() works behind the scenes.

βš™οΈ Step-by-Step: What Happens Inside require()?

When you write:

const user = require("./user");

Enter fullscreen mode Exit fullscreen mode

Node.js performs these steps:

  1. πŸ” Resolve Path – Finds the exact file to load.
  2. πŸ“¦ Check Cache – If already loaded, return it immediately.
  3. πŸ“„ Read File – Reads the module from disk.
  4. 🧩 Wrap Module – Wraps your code inside a function.
  5. ▢️ Execute – Runs the module code.
  6. πŸ’Ύ Cache Module – Stores the exported object in memory.
  7. πŸ”„ Return Exports – Returns module.exports to your code.

πŸ’» Example

user.js

console.log("User Module Loaded");

module.exports = {
  name: "Krati"
};

Enter fullscreen mode Exit fullscreen mode

app.js

const user = require("./user");

console.log(user.name);

Enter fullscreen mode Exit fullscreen mode

Output

User Module Loaded
Krati

Enter fullscreen mode Exit fullscreen mode

If you call require("./user") again, “User Module Loaded” won’t be printed because Node.js returns the cached module.

πŸ’‘ Why Does This Matter?

Understanding this process helps you:

  • ⚑ Write more efficient Node.js applications
  • 🐞 Debug module-related issues
  • 🀝 Understand shared module state
  • 🎯 Answer common backend interview questions confidently

🎯 Quick Challenge

What happens first when require("./user") is called?

A. Execute the file

B. Check the module cache

C. Return module.exports

πŸ‘‡ Drop your answer in the comments before scrolling!

πŸ“Œ Key Takeaway

require() does much more than just load a file.

It resolves the path, checks the cache, reads the file, wraps it, executes it, caches the exports, and finally returns them.

Understanding these internals makes you a better Node.js developerβ€”not just someone who uses the framework, but someone who knows how it works.

πŸ’¬ Question for you:

Which Node.js topic would you like me to explain next?

  • module.exports vs exports
  • Module Wrapper Function
  • Circular Dependencies
  • CommonJS vs ES Modules

Your suggestion might become the next article in this Backend Internals series. πŸš€

μ›λ¬Έμ—μ„œ 계속 β†—

μ½”λ©˜νŠΈ

λ‹΅κΈ€ 남기기

이메일 μ£Όμ†ŒλŠ” κ³΅κ°œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•„μˆ˜ ν•„λ“œλŠ” *둜 ν‘œμ‹œλ©λ‹ˆλ‹€