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

fix import from ES Module #92

Open il3ven opened 2 years ago

il3ven commented 2 years ago

The problem was that Node was treating files in dist/esm as CommonJS. The solution would be to rename all files in dist/esm to .mjs or add a package.json with type: module. This PR chooses the second option.

Fixes #80

sudiptosarkar commented 2 years ago

This does not work. I tried it and it has problems with just-performance:

file:///home/xtreme/Source/private/JsAM/node_modules/limiter/dist/esm/clock.js:1
import { performance } from "just-performance";
         ^^^^^^^^^^^
SyntaxError: Named export 'performance' not found. The requested module 'just-performance' 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 'just-performance';
const { performance } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.9.0
sudiptosarkar commented 2 years ago

This does not work. I tried it and it has problems with just-performance:

file:///home/xtreme/Source/private/JsAM/node_modules/limiter/dist/esm/clock.js:1
import { performance } from "just-performance";
         ^^^^^^^^^^^
SyntaxError: Named export 'performance' not found. The requested module 'just-performance' 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 'just-performance';
const { performance } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.9.0

Also, I tried to import it like it says in the error here in clock.js and then it can't figure out export:

/home/xtreme/Source/private/JsAcMoF/node_modules/just-performance/dist/esm/node.js:1
export { performance } from "perf_hooks";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1027:15)
    at Module._compile (node:internal/modules/cjs/loader:1063:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
il3ven commented 2 years ago

This does not work. I tried it and it has problems with just-performance:

Yes, it won't work right now. A similar fix needs to be done for just-performance too. To test it locally, go to node_modules/just-performance/dist/esm and add a package.json. Similar to what this PR adds for node-rate-limiter.

package.json

{
  "type": "module"
}
sudiptosarkar commented 2 years ago

This does not work. I tried it and it has problems with just-performance:

Yes, it won't work right now. A similar fix needs to be done for just-performance too. To test it locally, go to node_modules/just-performance/dist/esm and add a package.json. Similar to what this PR adds for node-rate-limiter.

package.json

{
  "type": "module"
}

I wish I had seen your comment before now. It would have saved me some time.. I figured out the same thing you suggested and it worked. :smiling_face_with_tear: I raised the corresponding PR: just-performance#4