DEV Community

Cover image for Understanding require vs import in JavaScript: A Practical Guide
Nishanthan K
Nishanthan K

Posted on

Understanding require vs import in JavaScript: A Practical Guide

JavaScript offers two primary ways to include external modules or files in your code: require and import. While both serve the same purpose, they belong to different module systems and have distinct features. Let’s dive into their usage, differences, and how to make the most of them in your projects.

What is require?

require is part of the CommonJS module system, which was traditionally used in Node.js before the introduction of ECMAScript Modules (ESM). It allows you to import modules, JSON files, or even other JavaScript files into your project.

const module = require('module-name'); // Importing a module


const fs = require('fs'); // Importing Node.js' built-in File System module
const data = require('./data.json'); // Importing a local JSON file
Enter fullscreen mode Exit fullscreen mode

Key Features of require

Synchronous Loading

Modules are loaded synchronously, meaning the code execution halts until the module is completely loaded. While this can ensure proper sequencing, it might slow down execution in environments where performance is critical.

const fs = require('fs'); // Code execution pauses until fs is loaded
console.log('Module loaded');
Enter fullscreen mode Exit fullscreen mode

Dynamic Loading

Unlike import, you can use require inside functions or conditional blocks, allowing for runtime decisions about which modules to load.

if (process.env.NODE_ENV === 'development') {
    const devTool = require('./devTool'); // Loaded only in development mode
}
Enter fullscreen mode Exit fullscreen mode

Versatile File Support

require can import JSON files and JavaScript files without additional setup.

Limitations of require

  • It’s synchronous, which can cause performance issues when working with large modules in applications that demand high responsiveness.
  • With the rise of modern JavaScript, many libraries and frameworks now favor ES6 modules (import) for their enhanced capabilities and compatibility with browsers.

What is import?

import is part of the ES6 module system (introduced in ECMAScript 2015) and is now the standard way to handle modules in JavaScript. It’s widely used in both browser environments and modern Node.js setups.

import something from 'module-name'; // Importing a module

import fs from 'fs'; // Importing Node.js' File System module in ESM
import data from './data.json'; // Importing a local JSON file
Enter fullscreen mode Exit fullscreen mode

Key Features of import

Asynchronous Loading

Unlike require, import doesn’t block code execution. Dependencies are resolved asynchronously, making it more suitable for modern, performance-oriented applications.

Static Imports

All import statements are resolved statically at compile time and must appear at the top level of your module. Conditional imports aren’t allowed.

// This will throw an error
if (process.env.NODE_ENV === 'production') {
    import './prodModule';
}
Enter fullscreen mode Exit fullscreen mode

To achieve conditional loading, dynamic imports can be used:

if (process.env.NODE_ENV === 'production') {
    const module = await import('./prodModule');
}
Enter fullscreen mode Exit fullscreen mode

Named and Default Imports

The import syntax allows for more flexibility in importing specific parts of a module or the entire module.

import { readFile } from 'fs'; // Named import
import fs from 'fs'; // Default import
Enter fullscreen mode Exit fullscreen mode

Tree Shaking

Modern bundlers like Webpack can remove unused imports (tree shaking) to optimize the final build size.

Using import in Node.js

By default, Node.js uses CommonJS (require). However, you can enable ES6 modules (import) by adding the following to your package.json file:

{
  "type": "module"
}
Enter fullscreen mode Exit fullscreen mode

After this, you can start using import syntax in your Node.js projects.

import fs from 'fs';
Enter fullscreen mode Exit fullscreen mode

If you need to use both require and import in the same project, you might encounter compatibility issues. To resolve this, use dynamic imports where needed:

const fs = await import('fs');
Enter fullscreen mode Exit fullscreen mode

When to use require or import

  • Use require if:

    • You’re maintaining legacy Node.js projects.
    • You need dynamic loading of modules based on runtime conditions.
  • Use import if:

    • You’re working with modern JavaScript or front-end frameworks like React.
    • You want to leverage features like tree shaking and asynchronous loading.
    • You’re targeting browsers or modern Node.js environments.

Conclusion

Understanding the differences between require and import is essential for any JavaScript developer. While require is a tried-and-true approach for Node.js applications, the modern import syntax offers superior performance and compatibility for current development standards.

In practice, stick with import for new projects, but keep in mind how require works, it’s still widely used and understanding it ensures you can navigate legacy codebases with confidence.

Top comments (0)