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

Importing from esm in Node.js is broken in v2.1.0 #80

Closed alanshaw closed 4 months ago

alanshaw commented 3 years ago

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.

olizilla commented 3 years ago

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

michael-land commented 3 years ago

experiencing the same in 2.1.0

LandonSchropp commented 2 years ago

Same here.

Mwni commented 2 years ago

Same

raxod502 commented 2 years ago

Same here---I had some trouble finding this thread, so here is another possible error message you can get, for the benefit of other Googlers:

(node:290874) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/raxod502/files/temp/limiter-bug/node_modules/limiter/dist/esm/index.js:1
export * from "./RateLimiter.js";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1022:15)
    at Module._compile (node:internal/modules/cjs/loader:1056:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:201:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:154:23)
    at async Loader.import (node:internal/modules/esm/loader:177:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
nigelis commented 2 years ago

Same

w0rldart commented 2 years ago

Fails on v14.18.3 too

cornopaez commented 2 years ago

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

yarfuo commented 2 years ago

Hello!

I struggling with the same error. I would be happy to help, but I'm not proficient in Node.js.

Thanks :)

sudiptosarkar commented 2 years ago

@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. :upside_down_face:).

kutsan commented 2 years ago

Friendly ping @jhurliman.

brandonros commented 1 year ago

friendly ping @jhurliman

sudiptosarkar commented 1 year ago

For anyone still waiting, limiter-es6-compat

alanshaw commented 1 year ago

@sudiptosarkar thanks 🙏

shuklaalok7 commented 1 year ago

@sudiptosarkar I am getting this error. May you please help with this? My import is,

import {RateLimiter} from "limiter-es6-compat"

image

sudiptosarkar commented 1 year ago

@shuklaalok7, Can you please do the following?

Also, does it have the same problem with limiter when running with typescript?

wayne5540 commented 1 year ago

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;
Nazaire commented 1 year ago

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.

brenc commented 1 year ago

Ran into this myself. @Nazaire your workaround worked for me. Thank you for posting that.

@jhurliman please let us know if you need help maintaining this project. I would be glad to help.

brenc commented 1 year ago

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.

nlwillia commented 6 months ago

It's a hack, but since the types are wrong, I used patch-package to strip this down to something that would work when installing locally.

limiter+2.1.0.patch

diff --git a/node_modules/limiter/dist/cjs/clock.js b/node_modules/limiter/dist/cjs/clock.js
index afe9ba8..436039f 100644
--- a/node_modules/limiter/dist/cjs/clock.js
+++ b/node_modules/limiter/dist/cjs/clock.js
@@ -1,11 +1,10 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.wait = exports.getMilliseconds = void 0;
-const just_performance_1 = require("just-performance");
 // generate timestamp or delta
 // see http://nodejs.org/api/process.html#process_process_hrtime
 function hrtime(previousTimestamp) {
-    const clocktime = just_performance_1.performance.now() * 1e-3;
+    const clocktime = performance.now() * 1e-3;
     let seconds = Math.floor(clocktime);
     let nanoseconds = Math.floor((clocktime % 1) * 1e9);
     if (previousTimestamp != undefined) {
diff --git a/node_modules/limiter/package.json b/node_modules/limiter/package.json
index 2b05cca..cd28267 100644
--- a/node_modules/limiter/package.json
+++ b/node_modules/limiter/package.json
@@ -3,12 +3,9 @@
   "description": "A generic rate limiter for the web and node.js. Useful for API clients, web crawling, or other tasks that need to be throttled",
   "version": "2.1.0",
   "author": "John Hurliman <jhurliman@jhurliman.org>",
-  "main": "./dist/cjs/index.js",
-  "module": "./dist/esm/index.js",
-  "browser": "./dist/esm/index.js",
   "exports": {
-    "import": "./dist/esm/index.js",
-    "require": "./dist/cjs/index.js"
+    ".": "./dist/cjs/index.js",
+    "./*.js": "./dist/cjs/*.js"
   },
   "repository": "git://github.com/jhurliman/node-rate-limiter",
   "bugs": {
@@ -24,9 +21,6 @@
     "prepack": "yarn ttsc -p tsconfig.json && ttsc -p tsconfig.cjs.json",
     "test": "jest src"
   },
-  "dependencies": {
-    "just-performance": "4.3.0"
-  },
   "devDependencies": {
     "@babel/core": "^7.13.16",
     "@babel/preset-env": "^7.13.15",
jhurliman commented 5 months ago

I have a proposed fix in https://github.com/jhurliman/node-rate-limiter/pull/96 if anyone wants to test it out before I merge

FabianFrank commented 1 month ago

@jhurliman are you planning to publish the fixed version to npm?