Node.js has traditionally used CommonJS modules, which rely on the require and module.exports syntax. However, with the introduction of ECMAScript 2015 (ES6), a new module system was introduced that uses the import and export keywords. In this tutorial, we will explore how to use ES6 modules in Node.js.
Enabling ES6 Modules
To use ES6 modules in Node.js, you need to enable them explicitly. There are several ways to do this:
- Using the
.mjsextension: You can rename your JavaScript files to have a.mjsextension. This tells Node.js to treat the file as an ES module. - Setting
type: "module"inpackage.json: You can add atypefield to yourpackage.jsonfile and set it to"module". This enables ES6 modules for all files in your project. - Using the
--experimental-modulesflag: You can run Node.js with the--experimental-modulesflag to enable ES6 modules.
Importing Modules
Once you have enabled ES6 modules, you can import them using the import statement. For example:
import express from 'express';
This imports the express module and assigns it to a variable named express.
Exporting Modules
To export a module, you can use the export keyword. For example:
export function greet(name) {
console.log(`Hello, ${name}!`);
}
This exports a function named greet that takes a name parameter and logs a greeting message to the console.
Mixing CommonJS and ES6 Modules
You can mix CommonJS and ES6 modules in your project. However, you need to be aware of some limitations:
- You cannot import a CommonJS module using the
importstatement. - You cannot export an ES6 module using the
module.exportssyntax.
To overcome these limitations, you can use tools like Babel or esm to transpile your code and enable interoperability between CommonJS and ES6 modules.
Using Babel
Babel is a popular transpiler that allows you to use modern JavaScript features in older environments. To use Babel with Node.js, you need to install the babel-register package and configure it to transpile your code.
Here’s an example of how to use Babel with Node.js:
require('babel-register')({
presets: ['env']
});
import express from 'express';
This code registers the babel-register package and configures it to transpile your code using the env preset. You can then import ES6 modules using the import statement.
Using esm
esm is a small package that allows you to use ES6 modules in Node.js without transpiling your code. To use esm, you need to install it and configure your Node.js start script to use it.
Here’s an example of how to use esm with Node.js:
npm install esm
You can then update your Node.js start script to use esm:
"scripts": {
"start": "node -r esm app.js"
}
This code runs your application using esm, which enables ES6 modules without transpiling your code.
Conclusion
Using ES6 modules in Node.js is a great way to take advantage of modern JavaScript features and improve the maintainability of your code. By enabling ES6 modules, importing and exporting modules, and mixing CommonJS and ES6 modules, you can write more efficient and scalable code. With tools like Babel and esm, you can overcome the limitations of older environments and use ES6 modules in production.