Any Node.js end-of-life day is a good day, but today especially so! Node.js v10 is being end-of-lifed today, so you can finally support ES6 imports in your JavaScript code examples and libraries by default!
The latest versions of Node.js v12 (not the earlier versions, mind you) support ES6 modules by default, so there is no need to do the ugly hacks you needed to do before if you wanted to write modern JavaScript.
Before
Before, you had to save your code a .mjs
instead of js
, and then run node
with the --experimental-modules
flag. Which most Node.js runtimes online were very slow to adopt.
$ node --experimental-modules index.mjs
After
The latest versions of Node.js v12+ support this out of the box, so there is no need to run with a flag. And most Node.js runtimes update to the latest Maintenance release, so they support it as well.
If you want to run on your own hosted environment now you can either:
- save the file with a
.mjs
extension, and run it regularly withnode index.mjs
- save the file with a regular
.js
extension, addtype: module
to yourpackage.json
file, and then run the file regularly withnode index.js
Changing Old Code
If you're migrating from the old require
to the more modern import
, there are a couple of things you'll need to do to your files:
- change
module.exports = avocados
intoexport default avocados
- change
const avocados = require('avocados')
intoimport avocados from 'avocados'
Most IDEs account for this and help you change it. Like VS Code.
If you want to see it in action on a slightly bigger project, I've switched over the Fidel sample application using things like express, dotenv and axios a little while back, it's all contained in a commit thought so easy to see the changes.
Wait, What is End-of-Life?
"End-of-life" or EOL is a term used by older, more enterprise-focused companies, to let everyone know that they're limiting support or outright not supporting a certain version of their product anymore. You could say the OpenJS Foundation has "pulled the plug" on Node.js v10. π
Got Love?
If you've loved the pun at the end, or if you just found this mildly useful, please consider following me on Twitter. I'd be storked. π
Top comments (0)