1
const rax = require('retry-axios');

Error [ERR_REQUIRE_ESM]: require() of ES Module

I was trying to implement a retry mechanism with retry-axios. After installing the package I got the Error [ERR_REQUIRE_ESM]: require() of ES Module error. I’m not too sure what this ESM is, I’ve never dealt with it before, therefore I don’t know what to do to fix this.

Sufail Kalathil
  • 558
  • 6
  • 21
  • ES modules use [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) instead of `require`. – Geshode Oct 21 '22 at 05:12
  • @Geshode Thank you. I have tried with import. But I got error. import * as rax from 'retry-axios'; SyntaxError: Cannot use import statement outside a module – Sufail Kalathil Oct 21 '22 at 05:16
  • Resolved by downgrading the retry-axios version to 2.6.0. https://www.npmjs.com/package/retry-axios/v/2.6.0 – Sufail Kalathil Oct 24 '22 at 13:18

1 Answers1

1

There're 2 main syntax of JS

  1. CJS - CommonJS (using const ... = require() )
  2. ESM - ES Module (using import ... from "" )

Some library supports only one syntax, some supports both. You can check library's main syntax from package.json if there's field "type": "module" it means it supports only ESM syntax You may need to use some alternative library

Pop Todi
  • 11
  • 1