Error [Err_Require_Esm]: How to Use Es6 Modules in Node 12?
From Node 12 Should Support Es6 Modules; However, I Just Keep Getting the Error: Question: How Do I Make a Mvp of Using Es6 Modules in Node 12? Package. Json {...
From Node 12 should support es6 modules; however, I just keep getting the error:
Question: How do I make a MVP of using es6 modules in node 12?
package.json
{
"name": "dynamic-es6-mod",
"version": "1.0.0",
"description": "",
"main": "src/index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"globby": "^10.0.1"
}
}
$ node -v
$ 12.6.0
$ npm run start
internal/modules/cjs/loader.js:821
throw new ERR_REQUIRE_ESM(filename);
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/dev/dynamic-es6-mod/src/index.mjs
at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:821:9)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
11 Answers
All you have to do is adding the flag --experimental-modules that supports the new es6 import/export statement also the order is important as the following.
"start": "node --experimental-modules src/index.mjs "
In Node 14 I solved it with workaround.
source of workaround:
short summary:
- your root level
package.jsondoesn't support ESM - subdirectory does - in
srcdirectory placepackage.jsonwith{ "type": "module" }
PS: ESLint team can't solve it easily right now, just because of core design... :(
The official documentation for the module states, that v2 should be used with require().
There is a work around though. Instead being imported it can be loaded asynchronously:
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
You have to add this line of code in your package.json file "type" : "module" You will be able to use imports statements instead of require if I get your question correctly.
Your package.json will look as follows:
{
"name": "dynamic-es6-mod",
"version": "1.0.0",
"description": "",
"main": "src/index.mjs",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"globby": "^10.0.1"
}
}
You can try with "jest-preset-angular": "11.0.0-rc.3".
It works in my case: Angular v13+ and node v16+.
I had a hell of a time with all this. I'm just posting my solution repo, in hopes it helps someone else.
This imports an ESM dependency (ora) into TypeScript without using babel.
Use previous or older version of your installed node module. It will work.
- Go to
package.jsonfile and write"type":"module", above debug like this:
"name": "npmmodule",
"version": "1.0.0",
"main": "index.js",
"type": "module",
- Use
importinstead ofrequire:
import chalk from 'chalk';
console.log(chalk.blue("hello world"));
Try using npm's esm module which will support es6 (It can also be used in production).
For Node.js:
Tell Node to ignore the error.
const Module = require('module')
const orig = Module._extensions['.js']
const fs = require('fs')
Module._extensions['.js'] = function (module, filename) {
try {
return orig(module, filename)
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
// From:
const content = fs.readFileSync(filename, 'utf8')
module._compile(content, filename)
// --
return
}
throw e
}
}
Use Babel to transpile on-the-fly.
require('@babel/register', {
ignore: (f) => {
// Don't ignore the package we want to transpile.
if (f.match('semver-regex') return false
// Prevent babel transpiling anything else in `node_modules`.
return f.match('node_modules')
},
overrides: [{
// Set preset to be used to transpile our ESM package.
test: /semver\-regex/,
presets: ['es2015'],
}]
})
// Call the rest of your code.
require('./index.js')
For Webpack:
Do similar but with Babel loaders and without the Node require hook.
Just need to update node:
run: nvm install 15 nvm use 15
credit: