Published on

Differences Between `require` and `import`

Authors
  • avatar
    Name
    hwahyeon
    Twitter
const express = require('express')
import express from 'express'

require and import both load Express, but use different module systems. Functionally, they are nearly the same.

Key Differences

  • Module System: require uses CommonJS, the default in Node.js. import uses ES6 modules, the modern JavaScript standard, and requires "type": "module" in package.json.
  • Compatibility: require is more compatible across environments, while import is recommended for modern setups.
  • Syntax: require is paired with module.exports, and import with export.

Use import for newer projects and require when compatibility is essential.