05 / 05

What is the command used to import external modules?

Difficulty: 2/10
module loading, CommonJS, ES modules

The “require” command is used for importing external libraries. For example - “var http=require (“HTTP”).” This will load the HTTP library and the single exported object through the HTTP variable

Scenario Questions

0-2 years experience

  1. 1You need to use the lodash library in a small script. How would you import it?
  2. 2If you forget to install a module and try to require it, what error do you see?
  3. 3Write the line of code you would use to import the built‑in 'fs' module.

2-5 years experience

  1. 1Your feature loads a JSON config file using require. It works locally but fails in production. What could cause the require to break?
  2. 2You need to load a module only when an environment variable is set. How would you write that import?
  3. 3Explain the practical differences between using require and import in a Node.js project that uses Babel.

5-8 years experience

  1. 1Your service loads many plugins at startup with require and you notice high latency. What strategies can reduce module‑loading overhead?
  2. 2You are refactoring a large codebase from CommonJS to ES modules. What challenges arise with the import syntax and how would you address them?
  3. 3Design a wrapper that abstracts module loading so the same code works with both require and import across Node and the browser.

8+ years experience

  1. 1Your organization plans to migrate all internal services to ES modules to enable tree‑shaking. What architectural considerations, tooling changes, and cross‑team coordination are required?
  2. 2When introducing a new module‑loading convention across multiple microservices, how do you ensure backward compatibility and avoid breaking existing deployments?
  3. 3Discuss the long‑term maintenance trade‑offs of standardizing on import versus require in a polyglot environment with legacy code.

Follow-up Questions

  • What error do you see if the required module cannot be found?
  • Can you load a module conditionally at runtime? How would you do it?
  • How does Node resolve the location of a package when you call require?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.