Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12? Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12? node.js node.js

Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12?


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: https://github.com/eslint/eslint/issues/12319#issuecomment-535799024

short summary:

  1. your root level package.json doesn't support ESM
  2. subdirectory does - in src directory place package.json with { "type": "module" }

PS: ESLint team can't solve it easily right now, just because of core design... :(


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"  }}