4

Node.js LTS version 14, released Oct 27, 2020, finally supports ES6 modules with the import/export syntax. You can convert all the files in a node project to modules by specifying the type field in package.json

// package.json
{ "type": "module" }

Or change the extension of the files from .js to .mjs (both importing and exporting files).
Then use the import/export syntax in place of module.exports/require()

This apparently doesn't work with the Jest testing framework. It still treats all files as commonjs and throws errors wherever import/export statements are placed. Has Jest just not caught up with this development in Node or is there some way to make it work without the overhead of adding babel?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Steve Carey
  • 2,876
  • 23
  • 34
  • Babel is not an overhead but a convenience. Jest ESM support is experimental. I'd expect it to be naturally more limiting than CJS. See https://jestjs.io/docs/en/ecmascript-modules – Estus Flask Oct 27 '20 at 20:21
  • Yeah, too bad. I was looking forward to no more module.exports/require() and no more babel with this latest Node release. Been fooling around with it for a while today and looks like the easiest solution is to just continue using the setup I've been using in the past. For Jest testing anyway. – Steve Carey Oct 27 '20 at 21:25

0 Answers0