Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Must use import to load ES Module #36

Closed
stwonary opened this issue Feb 15, 2022 · 5 comments
Closed

Must use import to load ES Module #36

stwonary opened this issue Feb 15, 2022 · 5 comments

Comments

@stwonary
Copy link

I'm using the sample code and got this error:

import pThrottle from 'p-throttle';

const now = Date.now();

const throttle = pThrottle({
	limit: 2,
	interval: 1000
});

Found similar issues here
standard-things/esm#868
knex/knex#3571

Must use import to load ES Module: /var/task/node_modules/p-throttle/index.js require() of ES modules is not supported. require() of /var/task/node_modules/p-throttle/index.js from /var/task/app.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /var/task/node_modules/p-throttle/package.json.

@stwonary
Copy link
Author

just found this article

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

but I have no idea how this "type": "module" gonna impact any legacy codes. any ideas?
I use Typescript and on some old dependency I still use require('keyword-extractor')

@sindresorhus
Copy link
Owner

Then I would recommend importing this package using await import(…) which does work in CommonJS.

@tony-schumacher
Copy link

@sindresorhus even with dynamic imports it produces an ERR_REQUIRE_ESM error.
Tried Node 16 and 18.

const { default: pThrottle } = await import('p-throttle');

Any tip?

@laygir
Copy link

laygir commented Dec 22, 2023

Seems to be working here in Node 18.18.0 with something like this;

const setupThrottling = async ({ limit, interval }) => {
  const { default: pThrottle } = await import('p-throttle');

  const throttle = pThrottle({
    limit,
    interval,
  });

  const throttled = throttle(async (callback) => callback());

  return throttled;
};
const useThrottling = await setupThrottling({ limit: 1, interval: 2000 });

useThrottling(() => somethingThrottled());

@oluwatobiiloba
Copy link

Having a similar issue here despite using all the recommended changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants