node-rate-limiter: Importing from esm in Node.js is broken in v2.1.0

Importing from esm in Node.js is broken in v2.1.0:

import { RateLimiter } from 'limiter'
         ^^^^^^^^^^^
SyntaxError: Named export 'RateLimiter' not found. The requested module 'limiter' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'limiter';
const { RateLimiter } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:105:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:151:5)
    at async Loader.import (node:internal/modules/esm/loader:166:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)

v2.0.1 was working.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 44
  • Comments: 22 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@jhurliman, Can we please merge just-performance#4 and #92?

Those two PRs will fix this issue (I’m trying to use this in my framework, and need this fix to be able to use it. 🙃).

I’ve worked around it by importing the CJS file explicitly.

import * as limiter from "./node_modules/limiter/dist/cjs/index.js";

It’s ugly but it works.

For some reason, node was importing the esm files and treating them to be CJS files.

Looking forward to this being fixed.

For those who are still blocked, you can try following code to use require instead of import.

import { createRequire } from "module";
const require = createRequire(import.meta.url);

const RateLimiter = require('limiter').RateLimiter;

For anyone still waiting, limiter-es6-compat

Same issue here. Having issues with node 16.13.2. As with others previously mentioned, pinning limiter to 2.0.1 works great.

Fails for me too on node v16.4.2. Pinning limiter@2.0.1 works.

Spoke too soon. @Nazaire’s method doesn’t work in a library (at least with yarn). Looks like I’ll have to use limiter-es6-compat.

@sudiptosarkar thanks 🙏