jhurliman/node-rate-limiter

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

alanshaw opened this issue ยท 23 comments

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.

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

experiencing the same in 2.1.0

Same here.

Mwni commented

Same

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)

Same

Fails on v14.18.3 too

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

Hello!

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

Thanks :)

@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. ๐Ÿ™ƒ).

Friendly ping @jhurliman.

friendly ping @jhurliman

For anyone still waiting, limiter-es6-compat

@sudiptosarkar thanks ๐Ÿ™

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

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

image

@shuklaalok7, Can you please do the following?

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

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;

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

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

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.

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",

I have a proposed fix in #96 if anyone wants to test it out before I merge

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