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

Solution 1:

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 "

Solution 2:

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... :(

Solution 3:

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));