question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for commonJS from npm for dev explicitly

See original GitHub issue

Describe the bug

I’m trying to use a dependency that does not use ES module https://unpkg.com/choices.js@9.0.1/public/assets/scripts/choices.js and while it works during the build it doesn’t work during the dev with the error

The requested module ‘/@modules/choices.js/public/assets/scripts/choices.js’ does not provide an export named ‘default’

I tried to use optimizeDeps but it doesn’t work.

  optimizeDeps: {
    include: ['choices.js']
  }

Reproduction

Install choices.js and try to use it during development.

Suggestion / Idea

Is there an option to add commonjs to ES Module conversion for specific dependencies ? Something like :

commonJS: ['choices.js', 'dep2']

System Info

  • vite version: 1.0.0-beta.11
  • Operating System: Linux
  • Node version: 14.5.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
yyx990803commented, Jul 16, 2020
  • h.js has a valid single ESM file so it doesn’t need to be optimized/wrapped as ESM - it’s loaded directly as-is.
  • m.js is CJS and therefore is optimized into ESM and then resolved from optimized modules. So yes it is because of the package names.
0reactions
shawntaxcommented, Jul 12, 2020

I tested this out with two other packages with “.js” in the name, h.js and m.js, with Vite during development.

👍 h.js worked just fine. ❌ m.js gave the same Uncaught SyntaxError: The requested module '/@modules/m.js/m.js' does not provide an export named 'default' error.

Based on h.js working as expected, it seems the resolver can handle packages with .js in the name just fine, and it comes down to how individual packages handle ESM compatibility (and how tools handle interopability)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Create a Hybrid NPM Module for ESM and CommonJS.
It supports a single source code base and creates a module that can be consumed by CommonJS and ESM apps and modules.
Read more >
Explicit ESM in Node.js with .mjs - DEV Community ‍ ‍
The straightforward answer to this is that having different file extensions allows you to be explicit in how you want to run your...
Read more >
cjsc - npm
Utility that compiles CommonJS (NodeJS) modules into a single JavaScript file suitable for the browser. Latest version: 1.0.11, ...
Read more >
How the module system, CommonJS & require works
Learn how the Node.js module system & CommonJS works and what does `require` do under the hood.
Read more >
Compile a package that depends on ESM only library into a ...
npm uninstall typescript && npm install --save-dev typescript@next ... all envs which support Node cache modules, * but, just in case, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found