AppDividend
Latest Code Tutorials

ES6 Modules in Node Tutorial With Example

0

ES6 Modules in Node Tutorial With Example is today’s topic. ES6 Modules are useful when we need to make objects, functions, classes or variables available to the outside world it’s as simple as exporting them and then importing them where needed in other files. With ES2015 (ES6) evolution, we get built-in support for modules in JavaScript like with CommonJS, in which each file is its module. If you do not know about import in es6, then check out my Javascript Import Statement guide.

ES6 Modules in Node

JavaScript has had modules for a long time. However, they were implemented via libraries, not built into the language. ES6 is the first time that JavaScript has built-in modules. ES6 modules are stored in files.

Exporting Module in Javascript

You can export members one by one. What’s not exported won’t be available directly outside the module. 

We are practicing this example in Node.js. So first, we need to create a file called app.mjs with the .mjs extension.

Node.js does not support ES6 Module functionality directly. So we need to use its experimental feature.

Write the following code inside the app.mjs file.

// app.mjs

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}

We are exporting two functions called squrt and square from the app.mjs module.

We will use the –experimental-modules flag can be used to enable features for loading ESM modules.

Now, create one more file called server.mjs and add the following code.

// server.mjs

import { sqrt, square } from './app';

const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);

Now, to run the above file, we need to add a flag called –experimental-modules.

So type the following command.

node --experimental-modules server.mjs

The output is following.

 

ES6 Modules in Browser and Node

There is also another way to run ES6 modules in node.js. But for that, you need to install babel dependencies.

First, go to your terminal and go to inside your project root and type the following command.

npm init -y

Then we need to install two node modules dependencies. So type the following command.

npm install babel-register babel-preset-env --save-dev

It will install the modules inside the node_modules folder.

Now, create a file called start.js write the following code inside the start.js file.

// start.js

require('babel-register')({
    presets: ['env']
});

module.exports = require('./app.js')

Now, create another file inside the root called process.js and add the following code.

// process.js

export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}

Finally, add the following code inside the app.js file.

// app.js

import { sqrt, square } from './process';
const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);

Now, we will run the start.js file, and we will see the output in the console.

 

ES6 Modules in Browser and Node Tutorial With Example

See, we get the same thing, but this time, we have used the babel libraries to transpile the ES6 code and make use of ES6 modules inside Node.js.

That means, we have seen the two ways to use ES6 modules on the server side or node.js side.

Import Complete Module

We can import the complete module using the following code inside the app.js file.

// app.js

import * as func from './process';
const a = func.sqrt(4);
const b = func.square(2);
console.log(a);
console.log(b);

It will give us the same output. We have a complete import module as a func.

Single Default Module Export

If your export file has only one default module, then you can do it following way.

Write the following code inside the process.js file.

// process.js

export default function square(x) {
    return x * x;
}

Now, you can import this file inside the app.js file using the following way without curly braces.

// app.js

import square from './process';
const b = square(2);
console.log(b);

The output will be in the console. But you can see in the code that when we used the import statement, we have not used the curly braces.

Modules in JavaScript

In Javascript, each module is a piece of code that is executed once it is loaded.

A module can import things from other modules. It refers to those modules via module specifiers.

Modules are singletons. Even if a module is imported multiple times, only a single “instance” of it exists.

Finally, ES6 Modules in Browser and Node Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.