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

Import { init } from 'buttercup' fails "expected to be CommonJS" #286

Closed
cullub opened this issue Aug 5, 2020 · 4 comments
Closed

Import { init } from 'buttercup' fails "expected to be CommonJS" #286

cullub opened this issue Aug 5, 2020 · 4 comments

Comments

@cullub
Copy link
Contributor

cullub commented Aug 5, 2020

I'm getting the following error:

file:///C:/Users/cullub/Desktop/myproject/myFile.js:1
import { Credentials, FileDatasource, Vault, init } from 'buttercup';
                                             ^^^^
SyntaxError: The requested module 'buttercup' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'buttercup';
const { Credentials, FileDatasource, Vault, init } = pkg;
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
    at async Loader.import (internal/modules/esm/loader.js:162:24)

However, that line is taken directly from the buttercup-core README.md. Maybe your package.json is missing a type: module?

@cullub
Copy link
Contributor Author

cullub commented Aug 5, 2020

Maybe related: nodejs/node#32137

@perry-mitchell
Copy link
Member

@cullub Ah, you're using node's new import support? I haven't experimented with this myself, but buttercup's exporting is completely correct. We export commonjs for node which is the standard, still.

You should try to do as the error suggests:

import buttercup from 'buttercup';
const { Credentials, FileDatasource, Vault, init } = buttercup;

Note that Node's documentation on the type field:

If the nearest parent package.json lacks a "type" field, or contains "type": "commonjs", .js files are treated as CommonJS. If the volume root is reached and no package.json is found, Node.js defers to the default, a package.json with no "type" field.

As the default is commonjs, I feel that there's no change needed. We don't export ES modules (only for the web). CommonJS is supported on all node versions whereas ES modules are not. We also don't plan to support a third build mode just for ES6+ import support, sorry.

I'll close this issue for now, but if overwhelming evidence to the contrary pops up, I'd happy reconsider my position on this. I think in the end we just want what's:

  • The easiest and simplest to maintain
  • Going to be used by the most developers

@HomemadeToast57
Copy link

Hey! It is 2 years later now. Due to the broad support of ES modules in Node in 2022, would you have any interest in forming a combined effort to update this project to support them. I would love to be part of this and would be happy to contribute! :) Lmk!

@perry-mitchell
Copy link
Member

Hey, I'm not 100% sure but it still seems like ESM is not supported in Electron: electron/electron#21457

I'd be happy to be corrected here of course.

But until they are supported I don't think changing Buttercup core to ESM is wise nor needed.

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

3 participants