node-rate-limiter icon indicating copy to clipboard operation
node-rate-limiter copied to clipboard

fix import from ES Module

Open il3ven opened this issue 2 years ago • 4 comments

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

il3ven avatar Jul 28 '22 20:07 il3ven

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 avatar Aug 02 '22 07:08 sudiptosarkar

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)

sudiptosarkar avatar Aug 02 '22 07:08 sudiptosarkar

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

il3ven avatar Aug 02 '22 07:08 il3ven

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

sudiptosarkar avatar Aug 02 '22 10:08 sudiptosarkar