I have found a lot of similar posts, but, I’ve not found one specifically for this scenerio (for p-retry
) where, the library does provide it’s own type-definitions, and has sample documentations doing the same, is updated to the latest version, yet when I import it:
import pRetry from 'p-retry';
it gives me the following error:
Error [ERR_REQUIRE_ESM]: require() of ES Module /homeapi/node_modules/p-retry/index.js from /homeapi/dist/mail/mailBase.client.js not supported.
Instead change the require of index.js in /homeapi/dist/mail/mailBase.client.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/homeapi/dist/mail/mailBase.client.js:16:19) {
code: 'ERR_REQUIRE_ESM'
}
I don’t exactly get what the error says entirely, however, I tried using dynamic modules:
const pRetry = import(‘p-retry’); // returns promise
// wherever I use pRetry, I do:
(await pRetry).default(....)
and it gives the same with additional exception trace:
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
Error [ERR_REQUIRE_ESM]: require() of ES Module /homeapi/node_modules/p-retry/index.js from /homeapi/dist/mail/mailBase.client.js not supported.
Instead change the require of index.js in /homeapi/dist/mail/mailBase.client.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/homeapi/dist/mail/mailBase.client.js:16:19) {
code: 'ERR_REQUIRE_ESM'
If possible I would like to avoid dynamic modules. Is there something I’m missing in configuring things in some config files?
I’m using Nestjs
(v9, latest), with typescript and node lts version (20.15.1 LTS). Any similar alternative npm-package suggestions would also be of great help.
1
this is just because you’re using an ESM-only package in a CommonJS project, not related with nestjs itself
duplicate of Unable to import ESM module in Nestjs
and several others
1