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

Not able to load the library in ESM context using named import #82

Open
Feiyang1 opened this issue Oct 27, 2021 · 1 comment
Open

Not able to load the library in ESM context using named import #82

Feiyang1 opened this issue Oct 27, 2021 · 1 comment

Comments

@Feiyang1
Copy link

When trying to load the library using and running the script in esm mode:

import { Client } from 'faye-websocket';

I got an error:

SyntaxError: Named export 'Client' not found. The requested module 'faye-websocket' 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 'faye-websocket';
const { Client } = pkg;

It suggests a workaround by using the default import first, then destruct the import, but it would be nice for the library to provide an esm entry point using the exports field.

@jcoglan
Copy link
Collaborator

jcoglan commented Oct 27, 2021

CommonJS modules can be imported from ESM modules using the import keyword. As you've noticed, you cannot "destructure" the module in the import statement, because it's not an ESM module, but import WebSocket from 'faye-websocket' should work and let you access everything else the package exports via the WebSocket binding.

I currently have no plans to add ESM support to my packages because, having tried it out, my impression is that it adds a substantial amount of complexity for packages to support both kinds of exports, in terms of building, testing and publishing the package. Given that CommonJS can be imported from ESM, I'm not convinced that complexity is worth it.

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

No branches or pull requests

2 participants