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
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
.mjsextension, and run it regularly with
- save the file with a regular
type: moduleto your
package.jsonfile, and then run the file regularly with
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:
module.exports = avocadosinto
export default avocados
const avocados = require('avocados')into
import 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. 😅
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)