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

Build Issue with CommonJS Version After Updating to choices.js v11 #1250

Open
roma-formio opened this issue Jan 6, 2025 · 0 comments
Open
Labels

Comments

@roma-formio
Copy link

Describe the bug
Our library supports both CommonJS (cjs) and ES Module (esm) builds. We are currently trying to update choices.js to v11 but are facing a build issue with the cjs version.

To Reproduce
Steps to reproduce the behavior:

  1. Go to StackBlitz example.
  2. Run npm run build:cjs and npm run servecommands.
  3. Inspect the console.
  4. See the error: Uncaught TypeError: choices_js_1.default is not a constructor.

This issue can also be reproduced with Node.js, as it uses cjs by default:

  1. Install the latest version of choices.js.
  2. Create an index.js file and try to import choices: const Choices = require('choices.js');.
  3. Run node index.js.
  4. See error
main.js:1
const Choices = require('choices.js');
                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module demo\node_modules\choices.js\public\assets\scripts\choices.js from domo\main.js not supported.
choices.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename choices.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in demo\node_modules\choices.js\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Expected behavior
We should be able to import choices.js and use it with cjs modules.

Screenshots
image

Choices version and bundle

  • Version: [e.g. v11.0.3]

Additional context
It looks like the problem is in the type: "module" declaration in the package.json. The issue is resolved if the type: "module" is removed in local version of choices.js, as the package.json contains both module and main entry points.

@roma-formio roma-formio added the bug label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant