diff --git a/docs/index.md b/docs/index.md index 7b3f9765da..4b59febae8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1527,7 +1527,7 @@ this means either ending the file with a `.mjs` extension, or, if you want to us adding `"type": "module"` to your `package.json`. More information can be found in the [Node.js documentation](https://nodejs.org/api/esm.html). -> Mocha supports ES modules only from Node.js v12 and above. Also note that +> Mocha supports ES modules only from Node.js v12.11.0 and above. Also note that > to enable this, you need to add `--experimental-modules` when running > Mocha. Last, but not least, the ESM implementation Mocha is not yet > stable so given that the specification in Node.js may diff --git a/lib/utils.js b/lib/utils.js index 45c89a870e..612af767d8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -843,7 +843,21 @@ exports.defineConstants = function(obj) { * @returns {Boolean} whether the current version of Node.JS supports ES Modules in a way that is compatbile with Mocha */ exports.supportsEsModules = function() { - return typeof document === 'undefined' - ? +process.versions.node.split('.')[0] >= 12 - : false; + if (typeof document !== 'undefined') { + return false; + } + + var versionFields = process.versions.node.split('.'); + var major = +versionFields[0]; + + if (major >= 13) { + return true; + } + if (major < 12) { + return false; + } + // major === 12 + var minor = +versionFields[1]; + + return minor >= 11; };