Friday 20 December 2019

Moving from Babel ES modules to pure Node.js 13.2

As of Node.js 13.2, ES modules are supported without a flag. I have a large project that supports ES module syntax with babel, and now I'd like to remove babel if possible. However, this doesn't seem to be as simple as adding "type": "module" to my package.json and ripping out babel.For one, none of my import statements include .js on the end. Most look like import Thing from './thing' to import the default export from the thing.js file. The Node implementation of ES modules seems to require the .js on the end (I get a Cannot find module... error without it). I'd love to not have to go around renaming every one of my imports to end with .js.I also have tons of modules organized as folders with an index.js in them which controls the public interface of the module. Every usage of this imports only the folder name (import Thing2, { otherThing } from './thing2 imports the default export from ./thing2/index.js). This also seems to give me a Cannot find module... error, and again, I'd like to not go change everything to import from ./thing2/index.js.Is what I'm doing not actually ES module spec compliant and just some extra Babel sugar? Is support for this in Node still forthcoming? Having trouble finding documentation to explain how/if Node is supposed to ever handle these two examples.

Submitted December 20, 2019 at 05:51PM by rca06d

No comments:

Post a Comment