Last Updated : 09 Sep, 2024
Summarize
Comments
Improve
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.
Syntax:
import name from 'module-name';
Below are two approaches to use the ES6 import in NodeJS:
Table of Content
- By changing content of package.json file
- By using the esm module
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:
To handle this issue, we use ES6 import. Let’s see how to use ES6 import in Node.js with examples.
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
Approach 1: 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.
To use ES6 imports in Node.js,
- set
"type": "module"
in yourpackage.json
file. - Use the
import
statement to include modules, ensuring file extensions like.js
are explicitly specified.
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", "dependencies": { "express": "^4.17.1" }}
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
//index.jsimport 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.
Output:
localhost:5000
Approach 2: By using the esm module
We can also enable ES6 import by installing esm module.
Step to install esm: write the following command
npm install esm
For Node versions older than 12: 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.
For newer node versions we can directly write node index.mjs to execute the task.
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", "dependencies": { "esm": "^3.2.25", "express": "^4.17.1" }}
Example: Create a file index.mjs and write the program using ES6 import.
//index.mjsimport 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 index.mjs
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.
Next Article
Import and Export in Node.js