Skip to solution
mediumFrontend

What are modules in JavaScript?

941 views
01

Understand the problem

Question presented to candidate: "If two different files both do require('./config') (or import from the same module), do they each get their own separate copy of whatever that module exports, or genuinely the same one?"

What a strong answer should cover:

  • 📌 Interview term: a module — a single file with its own private scope — variables/functions declared inside it are genuinely NOT visible outside unless deliberately exported, and code in other files must explicitly import whatever it needs.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: requiring/importing the SAME module from multiple places genuinely returns the identical, cached object reference every time, not a fresh copy — a module's top-level code genuinely runs only once, the first time it is loaded, no matter how many separate files import it afterward.
  • 📌 Interview term: the two real module systems in JavaScript — CommonJS (require/module.exports) and ES modules (import/export) — covered in much more depth, including their real structural differences (static vs. dynamic resolution), in this bank's own dedicated CommonJS-vs-ES-modules question.
  • A precise answer names the real, practical benefit modules provide beyond just "splitting files": genuine encapsulation — a module's internal helper variables genuinely cannot leak into or collide with another module's own internal variables, since each module has its own private top-level scope — a real, structural fix for the exact kind of global-scope pollution this bank's own IIFE question covers as the OLDER, pre-module workaround for the identical problem.
  • A precise answer names that a module's exports are genuinely the ONLY public interface — everything not explicitly exported stays genuinely private to that file, by default, with no special syntax (like an IIFE, covered in this bank's own dedicated question) required to achieve that privacy.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own repeated-import scenario (cached, not re-copied) is the strong signal.

Code / implementation expected: Yes — a real, direct require() caching demonstration (the same reference returned twice) is the clearest, most convincing proof.

moduleses modulescode organizationreusability
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The module-caching claim below was actually run and confirmed inside a real CommonJS script

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: requiring the same module from multiple places genuinely returns the identical, cached reference — its top-level code runs only once
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 57 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track