Skip to main content

What is the difference between require and import?

Junior NodeJS
Quick Answer require() is CommonJS รขโ‚ฌโ€ synchronous, loads at runtime, works everywhere in Node.js. import/export is ES Modules รขโ‚ฌโ€ asynchronous, static (evaluated at parse time), required for tree-shaking. In Node.js, ESM requires .mjs extension or "type": "module" in package.json. CommonJS is still more common; ESM is the future standard.

Answer

require is CommonJS; import is ES modules. Both load modules but with different syntax rules.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice