Skip to main content

Command Palette

Search for a command to run...

JavaScript Modules: Import and Export Explained

Updated
•View as Markdown
JavaScript Modules: Import and Export Explained

As your applications grow, one problem starts appearing very quickly.

Your code becomes too big to manage.

At the beginning, writing everything in a single file feels fine. But as features increase, that file becomes harder to read, harder to debug, and almost impossible to maintain.

This is where modules come in.


Why Modules Are Needed

Imagine building a real-world application.

You might have:

  • user-related logic

  • API calls

  • utility functions

  • validation logic

If all of this is written in one file, things get messy very fast.

Modules solve this problem by allowing you to split your code into multiple files and organize it properly.

Each file handles a specific responsibility.

This makes your code cleaner and easier to manage.


What Are Modules?

A module is simply a file that contains code you can reuse in other files.

Instead of writing everything together, you divide your code into smaller, focused parts.

Each module can:

  • export functionality

  • import functionality from other modules


Exporting Functions or Values

To use something from a file, you need to export it.

Named Export

// math.js

export function add(a, b) {
  return a + b;
}

export const PI = 3.14;

Here, multiple things are exported using their names.


Importing Named Exports

// app.js

import { add, PI } from "./math.js";

console.log(add(2, 3));
console.log(PI);

You import exactly what you need.


Default Export

Sometimes a file exports only one main thing.

// greet.js

export default function greet(name) {
  console.log("Hello " + name);
}

Importing Default Export

// app.js

import greet from "./greet.js";

greet("Shikhar");

Default exports do not require curly braces.


Named vs Default Export

The difference is simple.

Named exports allow multiple exports per file.

Default export is used when there is a single main export.

Named → import { add }
Default → import greet

How Modules Improve Code Organization

Instead of writing everything in one place:

// messy single file
function add() {}
function subtract() {}
function login() {}
function fetchData() {}

You organize it like this:

math.js → add, subtract
auth.js → login
api.js → fetchData

And then import where needed.

This makes your code structured and readable.


Real-World Example

// user.js

export function getUser() {
  return { name: "Shikhar" };
}
// app.js

import { getUser } from "./user.js";

const user = getUser();
console.log(user.name);

Each file has a clear purpose.


Module Flow (Mental Model)

You can think of it like this:

File A → exports → File B → imports → uses functionality

Files communicate through exports and imports.


Benefits of Modular Code

Modular code makes your application:

  • easier to read

  • easier to maintain

  • easier to debug

  • easier to scale

Instead of one large file, you have small, manageable pieces.


Common Mistakes

A common mistake is mixing default and named imports incorrectly.

Another is forgetting the correct file path.

Also, over-splitting code into too many modules can make things confusing.


Final Thought

Modules are not just a feature. They are a way of thinking.

Instead of writing everything together, you break your code into meaningful pieces.

This is how real-world applications are built.

Once you start using modules properly, your code becomes cleaner, more structured, and much easier to work with.

Production Ready JavaScript

Part 1 of 2

Production-Ready JavaScript is a practical series focused on helping you move from writing basic code to building real-world, scalable applications. You’ll learn best practices, clean code principles, performance optimization, debugging techniques, and patterns used by professional developers to write maintainable and efficient JavaScript code.

Up next

Template Literals in JavaScript

Writing Cleaner and More Powerful Strings