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

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

Closed
alanshaw opened this issue May 20, 2021 · 23 comments · Fixed by #96 · May be fixed by #92
Closed

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

alanshaw opened this issue May 20, 2021 · 23 comments · Fixed by #96 · May be fixed by #92

Comments

@alanshaw
Copy link

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.

alanshaw pushed a commit to nftstorage/nft.storage that referenced this issue May 20, 2021
@olizilla
Copy link

olizilla commented Jul 21, 2021

Fails for me too on node v16.4.2. Pinning [email protected] works.

@michael-land
Copy link

experiencing the same in 2.1.0

@LandonSchropp
Copy link

Same here.

@Mwni
Copy link

Mwni commented Nov 1, 2021

Same

@raxod502
Copy link

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)

airforce270 added a commit to airforce270/CytubeBot that referenced this issue Jan 2, 2022
- Upgrade eslint
- Lock limiter to 2.0.1
  (jhurliman/node-rate-limiter#80)
@nigelis
Copy link

nigelis commented Jan 12, 2022

Same

@w0rldart
Copy link

Fails on v14.18.3 too

@cornopaez
Copy link

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

@yarfuo
Copy link

yarfuo commented Feb 22, 2022

Hello!

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

Thanks :)

@sudiptosarkar
Copy link

@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. 🙃).

@kutsan
Copy link

kutsan commented Aug 6, 2022

Friendly ping @jhurliman.

@brandonros
Copy link

friendly ping @jhurliman

@sudiptosarkar
Copy link

For anyone still waiting, limiter-es6-compat

@alanshaw
Copy link
Author

@sudiptosarkar thanks 🙏

@shuklaalok7
Copy link

shuklaalok7 commented Apr 26, 2023

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

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

image

@sudiptosarkar
Copy link

@shuklaalok7, Can you please do the following?

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

@wayne5540
Copy link

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
Copy link

Nazaire commented Jul 25, 2023

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
Copy link

brenc commented Aug 8, 2023

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
Copy link

brenc commented Aug 9, 2023

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
Copy link

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 <[email protected]>",
-  "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
Copy link
Owner

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

@FabianFrank
Copy link

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

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