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

Fix ES module for Node.js #962

Merged
merged 19 commits into from
May 24, 2022
Merged

Conversation

denysoblohin-okta
Copy link
Contributor

@denysoblohin-okta denysoblohin-okta commented May 16, 2022

Issue:
Can't import broadcast-channel as ES module for Node.js.

import { BroadcastChannel } from 'broadcast-channel';
         ^^^^^^^^^^^^^^^^
SyntaxError: Named export 'BroadcastChannel' not found. The requested module 'broadcast-channel' 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 'broadcast-channel';
const { BroadcastChannel } = pkg;

    at ModuleJob._instantiate (internal/modules/esm/module_job.js:124:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:179:5)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    at async handleMainPromise (internal/modules/run_main.js:59:12)

Steps to reproduce: create simple project with index.mjs (having just 1 line with import of broadcast-channel) and try to run node index.mjs

Solution:

  • Use .mjs extensions for ESM module. (Don't add type: "module" in package.json because we want to support both ESM and CJS)
  • Add testing ESM and CJS modules

More details here: https://github.com/sheremet-va/dual-packaging

@denysoblohin-okta
Copy link
Contributor Author

@pubkey Can you review please?

@pubkey
Copy link
Owner

pubkey commented May 23, 2022

Please do not check in generated files from the dist folder. This makes the PR unreadable.

@denysoblohin-okta
Copy link
Contributor Author

@pubkey Done

@pubkey
Copy link
Owner

pubkey commented May 23, 2022

Hmm, the CI fails.
But it also fails on master.
Looks like a timing problem where github actions now gives a slower server then before.

@denysoblohin-okta
Copy link
Contributor Author

@pubkey Probably need to increase this constant:

if (t > 50) {
throw new Error('this should never happen');

Should I fix this to make CI pass?

@pubkey
Copy link
Owner

pubkey commented May 24, 2022

Yes please.

@denysoblohin-okta denysoblohin-okta force-pushed the support-esm-node branch 3 times, most recently from 1ee4f8b to c14f3a6 Compare May 24, 2022 21:07
@denysoblohin-okta
Copy link
Contributor Author

@pubkey Done. CI passes. Please approve workflow for PR.

@pubkey pubkey merged commit 930cf6b into pubkey:master May 24, 2022
pubkey added a commit that referenced this pull request May 24, 2022
@pubkey
Copy link
Owner

pubkey commented May 24, 2022

Merged and released.
Thank you.

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

Successfully merging this pull request may close these issues.

2 participants