Require and Import
In Node.js, require and import are both used to bring modules or packages into our code, but they come from different module systems and have distinct behaviors. Here’s a breakdown of their differences and usage:
1. Module Systems
require: Part of the CommonJS module system, which is the default module system in Node.js.import: Part of the ES6 (ESM) module system, which is ECMAScript’s standard syntax for importing/exporting modules.
2. Syntax and Usage
requireSyntax:const module = require('module-name');requirecan be used anywhere in the code (even conditionally), making it more flexible in certain cases. It loads modules synchronously.importSyntax:import module from 'module-name';importmust be at the top level of the module and is loaded asynchronously. It requires the file to be declared as an ES module (with"type": "module"inpackage.jsonor using the.mjsfile extension).
3. Module Exporting
module.exports/exports: When usingrequire, we define exports withmodule.exportsorexports.export default/export { ... }: When usingimport, we define exports usingexport default(for a single default export) orexport { ... }for named exports.
4. Compatibility
require: Works in all Node.js versions and is generally more compatible with the existing Node.js ecosystem.import: Requires Node.js 12+ with"type": "module"inpackage.jsonor.mjsfiles, aligning more with front-end JavaScript’s module system.
5. Use Cases
- Use
requirewhen working with CommonJS modules, such as many Node.js packages that still use CommonJS by default. - Use
importwhen we want to work with ES6 modules and take advantage of asynchronous loading and more modern syntax.
Example Comparison
Using require:
// CommonJS
const fs = require('fs');
fs.readFileSync('path/to/file');
Using import:
// ES6 Modules
import fs from 'fs';
fs.readFileSync('path/to/file');
In summary, use require for traditional CommonJS projects and import for projects configured to use ES6 modules. Both are valid in Node.js, but ES6 modules (import/export) are the standard moving forward.