Key differences between require(node.js) and import(javascript)


What is require

Require permits you to load other modules or files into your application.

Syntax

let example = require('./example.js')

What is import

Import is used to import modules where you can selectively load them when you need. They are permitted only in ES modules.

Syntax

import Example from './example.js';

Differences between Require and Import

RequireImport
Loading is SynchronousImport is Asynchronous
Throws error at runtimeThrows error while parsing
NonlexicalLexical
Can be called conditionallyAlways declared at the beginning of the program