To use ES6 import syntax in Node.js, you must enable ES6 modules to support, and there are two ways to do that:
- Use the file extension .mjs for your module files and run Node.js with the –experimental-modules flag.
- Add “type”: “module” to your package.json file and use the .js extension for your module files.
Method 1: Using .mjs file extension
ES6 module syntax allows you to export modules from one JavaScript file using the export keyword.
Create an app.mjs file and add the below code in it.
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
We export two functions, sqrt, and square, from the app.mjs module.
We will use the –experimental-modules flag can be used to enable features for loading ESM modules.
Create another file called server.mjs and add the following code.
import { sqrt, square } from './app';
const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);
To run the above file, we need to add a flag called –experimental-modules.
Type the following command.
node --experimental-modules server.mjs
Output
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 inside your project root and type the following command.
npm init -y
Then we need to install two node module 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.
Next, write the following code inside the start.js file.
require('babel-register')({
presets: ['env']
});
module.exports = require('./app.js')
Create another file inside the root called process.js and add the following code.
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
Finally, add the following code inside the app.js file.
import { sqrt, square } from './process';
const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);
We will run the start.js file and see the output in the console.
We get the same thing, but this time, we have used the babel libraries to transpile the ES6 code and use ES6 modules inside Node.js.
That means we have seen the two ways to use ES6 modules on the server or node.js sides.
Import Complete Module
We can import the complete module using the following code inside the app.js file.
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. So we have a complete import module as a func.
Single Default Module Export
If your export file has only one default module, you can do it the following way.
Write the following code inside the process.js file.
export default function square(x) {
return x * x;
}
You can import this file inside the app.js file using the following way without curly bracing.
import square from './process';
const b = square(2);
console.log(b);
The output will be 4 in the console. But you can see in the code that when we used the import statement, we have not used the curly braces.
Method 2: Using the “type”: “module” method
Step 1: Add the “type”: “module” inside the package.json file.
In the package.json file, add “type” : “module”. Adding this enables ES6 modules.
The package.json file should look like this.
{
"name": "helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Step 2: Create a file server.js and export two modules.
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
Step 3: Import the “server.js” module inside the “app.js” file
Write the following code inside the app.js file.
import { sqrt, square } from './server.js';
const a = sqrt(4);
const b = square(2);
console.log(a);
console.log(b);
Output
2
4
And we successfully imported the ES6 module inside Node.js without any errors.
That’s it.
Thank you for the article!
I’ve added an example using with Typescript if someone needs it: https://github.com/Urigo/typescript-node-es-modules-example