JavaScript

How to import/export ES6 modules in Node

Advertisements

If you have been following the javascript ecosystem for a while, you already know that even though ES6 modules are a thing, you still cannot import/export ES6 modules in node.js as of today.

Node 10 experimental flag for importing/exporting ES6 modules in Node

Though Node 10 has added an experimental flag (–experimental-modules) which allows you to use this functionality, upon using this functionality, you will soon realize that it is not that useful. You need to write your import/export statements in files which are named as “.mjs” instead of js files. Though that is because of the way ES6 modules are being implemented, but as a javascript developer, I should be able to keep using things I have been using and the modules should just work.

Even if I jump ship and start using the experimental flag for new projects that I am creating, using ES6 modules in node is still not that easy. As soon as you reference an npm package, you will get an error saying that the code being referenced is not in the Michael Jackson script format aka their file extensions are not ending with “.mjs”. Now I cannot control the npm registry. So this experimental flag usage was not something that I could have used in any project since a node project without npm packages is pretty much useless these days. And I will never work on getting the interoperability among these two correct since there are better ways out there.

Update 11/21/2019: Node 13.2.0 ships support for ECMASCRIPT modules and you no longer require the experimental flag, even though the implementation is still experimental and subject to change. You can read this post to know more about the announcement.

What other options do I have?

Babel

The other option would have been to use babel as we have been doing for ages. Babel would then transpile our ES6 code to ES5 code and then we can run our server using the transpiled code. I have previously written about using Babel to import/export ES6 modules using Node, but setting up Babel is a pain and an additional step which I would prefer avoiding whenever I can.

ESM

John-David Dalton has created a super easy to use npm package called esm which allows you to use tomorrow’s ES6 modules in node today! It is a zero-config solution which just works.

Using esm

You do not need to do much to use esm in your project. Firstly, you need to install it in your project.

  • For new projects, Run npm init esm or yarn create esm depending on which package manager you prefer
  • For existing projects, yarn add esm or npm install esm.

After that, you need to do is require this package when starting your server with node. For doing so you can use the require command line option when running your server. So for running the node server for the index.js file, you would use the command node -r esm index.js

If you don’t want to modify the command line parameters, you can require the esm module in a separate file. So, create a new file, say server.js and its contents would be:

require = require("esm")(module/*, options*/)
module.exports = require("./index.js")
JavaScript

And then you can run server.js using node as you normally would.

If you are looking to get some more insights about esm and it’s internals, this video should help you understand more:

Even if you don’t watch it, you are not missing on much. You don’t need to understand the implementation details and can easily keep using import/export for your ES6 modules in node if you followed the steps that I had mentioned above. So go ahead and remove dependencies to babel in your node project now and enjoy writing the future modules today!

Share this post with other fellow node developers to help them avoid the hassle of using commonjs in their node projects.

Sponsored:

Take a small step towards being a professional web developer by migrating your web development/designing environment tools onto the cloud Hosted Citrix vdi at an affordable citrix xendesktop pricing from CloudDesktopOnline. from CloudDesktopOnline. Learn more about QuickBooks Hosting and Office 365 Enterprise E1 suite by visiting Apps4Rent.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

View Comments

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 month ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements