jhurliman / node-rate-limiter

A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled
MIT License
1.5k stars 132 forks source link

Got ERR_REQUIRE_ESM #78

Closed blueberry6401 closed 3 years ago

blueberry6401 commented 3 years ago

Hi, I got this message

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: …/limiter/dist/cjs/index.js
require() of ES modules is not supported.
require() of …/limiter/dist/cjs/index.js from MyService.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.

When trying to require const RateLimiter = require('limiter').RateLimiter;

Node: v15.14.0 Limiter: 2.0.1 Note that I previously used v1.1.3 and this does not happen.

yashwason commented 3 years ago

It appears that they have changed the package to ES modules. This requires that you now import the package into your app using import { RateLimiter } from "limiter";

Instead of the old way const RateLimiter = require(limiter).RateLimiter;

Although it's still not clear why the maintainers made the decision to change this. It would help if they added a CHANGELOG

orgads commented 3 years ago

It also requires the application to be a module.

jhurliman commented 3 years ago

This library is now distributed using both CommonJS and ESM formats. It looks like Node.js is pulling in the CommonJS file (see the /cjs/ file path) which uses require() statements, but the "type": "module" in package.json is forcing it into ESM mode. https://github.com/jhurliman/node-rate-limiter/pull/79 should fix this.