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.

Transforming ES modules to Commonjs for Electron and Node

See original GitHub issue

Is your feature request related to a problem? Please describe.

Can Vite add support for transforming ES modules to CommonJS for Electron and Node?

For these two environments, ES modules are not supported (or do not have first-class support like the browsers). For that reason, the output code should use require and exports instead of using import and export.

For the dev mode, this transformation is necessary.

For the production model, if Rollup is able to bundle everything, it will be less of an issue, but there are still situations that the code uses dynamic requires which Rollup has issues bundling.

Describe the solution you’d like Adding a transformer that converts import/exports back to commonjs in the last step.

Describe alternatives you’ve considered

Additional context

As the owner of atom-community I was planning to use Vite for the Atom packages in our organization. But the issue is that for Atom packages, we need to use commonjs because Electron doesn’t support import/exports.

There was a similar issue here (#2218), but the solution mentioned there isn’t general because we need to list all the modules that use require manually.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
nomagickcommented, Apr 14, 2021

Hey @aminya for dynamic require(), try this:

patch-vite.ts

const hack1 = 'req';
const hack2 = 'uire';
const theRequire: any = window[hack1 + hack2 as any];
export default function patchedRequire(module: string): any {
    return theRequire(module);
}
1reaction
nomagickcommented, Mar 5, 2022

Vite is determined to screw your codes with require. After all the major focus of Vite is to “promote” esm. Not possible to compact with the thing that you are trying to replace, let it BURN

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrade your Node app: From CommonJS to ES Modules
Step by step: From CommonJS to ES Modules · 1) Prepare your code · 2) Update the package.json · 3) Convert all imports...
Read more >
Using ECMAScript modules (ESM) with Node.js
Learn about using ES modules in Node.js today and get a closer look at how you can migrate your codebase to make use...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
In Node 14, there are now two kinds of scripts: there are old-style CommonJS (CJS) scripts and new-style ESM scripts (aka MJS).
Read more >
Use ES modules in Electron - React.js Examples
Because Electron does not support ES Modules, and the Node ecosystem is shifting to ESM we are lacking an easy way to simply...
Read more >
How can I make the ESM work with electron in my project?
how to use es module but not commonjs with electron 16? 1 · Sharing an ESM ".js" module within Node / Electron environment....
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