DEV Community

Cover image for Working with modules in JavaScript.
Swastik Yadav
Swastik Yadav

Posted on • Edited on

7 4

Working with modules in JavaScript.

Hello Everyone,

In this post we will explore the modern way of using modules in JavaScript.

There are several ways to use modules in JavaScript:

  • AMD: One of the most ancient module system.
  • CommonJS: The module system created for Node.JS server.
  • UMD: Suggested as universal system. Compatible with AMD and CommonJS.
  • Language level module system (import / export): The modern way to use modules in JavaScript.

In this post we will only cover the last one as the first three are part of history, seen in older scripts.

What is module?

A module is just a file. To manage a large codebase different scripts are separated into different modules. Modules let's you call function of one script from another script.

  • export: Variables and functions labled with export, are accessible from outside the current script.
  • import: Allows to use functionality of other script in current script.

For instance:

sayHello.js

export function sayHello(user) {
  console.log(`Hello, ${user}`);
}
Enter fullscreen mode Exit fullscreen mode

main.js

import { sayHello } from "./sayHello.js";

sayHello("Swastik");

// Running main.js consoles "Hello Swastik".
Enter fullscreen mode Exit fullscreen mode

Modules work only via HTTP(s), not locally.
If you try to use modules via file:// protocol, import / export directives don't work. It only works via HTTP(s) protocol.

The code snippet in last example don't actually work, in order to make it work, we need to tell JavaScript that we are using modules. There are two ways to do that.

  1. Use .mjs as script file extension instead of .js.
  2. Specify "type": "module" in package.json.

Export and Import

Now, let's see some most common patterns and ways to use import / export in our scripts.

1. Name Export:

Export

export const name = "Value";
Enter fullscreen mode Exit fullscreen mode

Import

import { name } from "...";
Enter fullscreen mode Exit fullscreen mode

2. Default Export:

Export

export default "Value";
Enter fullscreen mode Exit fullscreen mode

Import

import anyName from "...";
Enter fullscreen mode Exit fullscreen mode

3. Rename Export

Export

const name = "value";
export { name as newName };
Enter fullscreen mode Exit fullscreen mode

Import

import { newName } from "...";
Enter fullscreen mode Exit fullscreen mode

4. Export List + Rename

Export

export {
  name1,
  name2 as newName2
}
Enter fullscreen mode Exit fullscreen mode

Import

import {
  name1 as newName1,
  newName2
} from "...";
Enter fullscreen mode Exit fullscreen mode

That's it for this post.

I run a newsletter where I share epic content on building your skillset. Read the previous issues here https://swastikyadav.com

Thank You!

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

Collapse
 
pachverb profile image
new/bird

You know how to package a module declaration code in some modular building tools, such as Webpack, gulp, etc. Instead of manually declaring a module.

Collapse
 
swastikyadav profile image
Swastik Yadav • Edited

I haven't done that before, but I will look into it, and get back to you if I find something.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay