Open In App

How to use an ES6 import in Node.js?

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

ES6 or ECMAScript 2015 is the 6th edition of the ECMAScript language specification standard. It defines the standard on how to implement JavaScript.

ES6 introduced a standardized syntax for importing and exporting modules in JavaScript which we will cover in this guide.

The import statement is used to import modules exported by some other module. A module is a file that contains a piece of reusable code. The import modules are in strict mode whether it is declared or not.

ES6 import Syntax

import name from 'module-name';

Different Ways to Use ES6 Import in JavaScript

Here we have covered all possible cases for using ES6 import statements in Node.js

Import Operation Syntax
Import entire module import * as name from ‘module-name’
Import default export from a module import name from ‘module-name’
Import single export from a module import { name } from ‘module-name’
Import multiple exports from a module import { nameOne , nameTwo } from ‘module-name’
Import a module for side effects only import ‘./module-name’

Node.js doesn’t support ES6 import directly. If we try to use import for importing modules directly in Node.js it will throw out the error.

For example, if we try to import express module by writing import express from ‘express’ Node.js will throw an error as follows:

import error

To handle this issue, we use ES6 import. Let’s see how to use ES6 import in Node.js with examples.

How to enable ES6 Import Syntax in Node.js

Node.js has experimental support for ES modules. There are two methods to use ES6 import statement in Node.js. We two methods given below

  • Changing content in package.json file
  • Using the esm module

By changing content of package.json file

To enable the use of ES6, we need to make some changes to the package.json file. Before following the steps make sure that Node.js is installed.

Below are the steps to achieve the same.

In the package.json file add “type”: “module”. Adding this enables ES6 modules.

The package.json file should look like this:

//package.json
{
"name": "index",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Example

Create a file index.js and write the program using ES6 import. For example, let’s try to import express in index.js file

Javascript




//index.js
 
import express from 'express';
const app = express();
 
app.get('/', (req, res) => {
    res.send('GeeksforGeeks');
})
const PORT = 5000;
 
app.listen(PORT, () => {
    console.log(`Running on PORT ${PORT}`);
})


Now run the index.js file by typing node –experimental-modules index.js in the terminal. 

running .js file

Output:

output after running .js file

localhost:5000

By using the esm module

We can also enable ES6 import by installing esm module.

To install esm, write the following command

npm install esm

Create a file with .mjs extension. If we are using the file with .mjs extension then we don’t have to add “type”: “module” in the package.json file. We can directly write the program and can execute it by typing node –experimental-modules index.mjs in the terminal.

Now, when using a file with .mjs extension the package.json file will look like this:

// package.json when using .mjs file
{
"name": "index",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Example

Create a file index.mjs and write the program using ES6 import.

Javascript




//index.mjs
 
import express from 'express';
const app = express();
 
app.get('/', (req, res) => {
    res.send('GeeksforGeeks');
})
const PORT = 5000;
app.listen(PORT, () => {
    console.log(`Running on PORT ${PORT}`);
})


Note: In the file server.js, we are importing the index.js file which holds the actual program which needs to be executed. 

node server.js

Output:

execution output

localhost:5000

Advantages of using import in place of require in Node:

  • Import helps in selectively loading the pieces of code that are required which helps in saving memory.
  • In case of require loading is synchronous whereas import can be asynchronous so it performs better than required.

Conclusion

ES6 or ECMAScript 6 is designed for JavaScript language standardization. Using import statements in Node.js would earlier give an error, but after enabling ES6, we can easily use import statements in Node.js.

This tutorial, explains how to use ES6 import in Node.js. We have explained 2 ways to use import in Node.js with examples. Now easily import and export modules between files.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads