Skip to the solution
MediumFrontend6 min read

What are modules in JavaScript?

941 views

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.

  • modules
  • es modules
  • code organization
  • reusability

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: requiring the same module from multiple places genuinely returns the identical, cached reference β€” its top-level code runs only once

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 57 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is lexical scoping and the scope chain?

Continue