By Hugo Agbonon (@codeheroics)


CommonJS: What node.js has been using
const fs = require('fs')
const { networkInterfaces } = require('os')
const msg = 'Hello'
module.exports = { msg }
ES Modules: The standard from now on
import fs from 'fs'
import os, { networkInterfaces } from 'os'
export const msg = 'Hello'
Seems simple to move from one to the other, right?
The same code won't work the same way whether it's in an ES Module or a CommonJS Module
import and export is not enough: You can have ES modules without those keywordsconsole.log(`Module loaded ${Date()}`)
is a valid ES Module
'use module''use module''use module'The current official node proposal is for .mjs

required or imported


What if automatic detection was possible after all?


A module requires an import or an export
All modules are parsed. If it doesn't contain any of these statements, it is not an ES Module.
console.log(`Module loaded ${Date()}`)
Would be recognized as a CommonJS module, while
console.log(`Module loaded ${Date()}`)
export {}
would be an ES module.
.js files, without ambiguities, without package.json setup (except a single field for libraries)
<script type="module"> resolves problems.export and import statements in modules...