[ERR_REQUIRE_ESM]: require() of ES Module … is not supported

Project setup

  • Electron app using electron-builder, which uses vite as build system. The build system is still almost identical to the template.
  • TypeScript, with the .ts file extension
  • ESM and import everywhere in our code. We do not use require() anywhere in our code base.
  • Obviously, we’re using various third-party npm libraries.

Reproduction

  1. npm create @quick-start/electron@latest
  • Project name: electron-app
  • Select a framework: › svelte
  • Add TypeScript? Yes
  • Add Electron updater plugin? Yes
  • Enable Electron download mirror proxy? No
  1. cd electron-app; yarn install; yarn run dev — works
  2. yarn add electron-context-menu, and import it to main.ts, as shown below.
  3. Start the app using yarn run dev

Note: This is an ESM-only (!) third-party npm library, not built by me. The problem is not limited to this library. I have seen the same error multiple times in the past, with different libraries. It appears to be a generic problem with vite in combination with ESM-only modules.

Actual result

  • Error, see below for console output

Expected result

(and actual result before adding electron-context-menu)

  • App starts without errors

Code

Reduced code of the e2/src/main/index.ts file:

import { BrowserWindow } from 'electron'
import contextMenu from 'electron-context-menu'; // <-- fails here

function createWindow(): void {
  contextMenu({});
  const mainWindow = new BrowserWindow({...})
  ...
}

createWindow();
...

You find the rest of the files, including the build config files, at https://github.com/mustang-im/mustang/tree/4fc959d/e2

Error

App threw an error during load
Error [ERR_REQUIRE_ESM]: require() of ES Module /e2/node_modules/electron-context-menu/index.js from /e2/out/main/index.js not supported.
Instead change the require of /e2/node_modules/electron-context-menu/index.js in /e2/out/main/index.js to a dynamic import() which is available in all CommonJS modules.
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at Object.<anonymous> (/e2/out/main/index.js:37:21)
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at loadApplicationPackage (/e2/node_modules/electron/dist/resources/default_app.asar/main.js:121:16)
    at Object.<anonymous> (/e2/node_modules/electron/dist/resources/default_app.asar/main.js:233:9)
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at node:electron/js2c/browser_init:2:123937
    at node:electron/js2c/browser_init:2:124140
    at node:electron/js2c/browser_init:2:124144
    at f._load (node:electron/js2c/asar_bundle:2:13377)
A JavaScript error occurred in the main process
Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /e2/node_modules/electron-context-menu/index.js from /e2/out/main/index.js not supported.
Instead change the require of /e2/node_modules/electron-context-menu/index.js in /e2/out/main/index.js to a dynamic import() which is available in all CommonJS modules.
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at Object.<anonymous> (/e2/out/main/index.js:37:21)
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at loadApplicationPackage (/e2/node_modules/electron/dist/resources/default_app.asar/main.js:121:16)
    at Object.<anonymous> (/e2/node_modules/electron/dist/resources/default_app.asar/main.js:233:9)
    at f._load (node:electron/js2c/asar_bundle:2:13377)
    at node:electron/js2c/browser_init:2:123937
    at node:electron/js2c/browser_init:2:124140
    at node:electron/js2c/browser_init:2:124144
    at f._load (node:electron/js2c/asar_bundle:2:13377)

Fix attempts

What I tried to fix it:

  • Error msg suggests to “change the require of …”: We’re not using require() at all. Very confusing and misleading error msg. I suspect vite converts the import into a require()?
  • I’ve tried with and without type: module in e2/package.json (of the electron app) – same result. Does not fix it.
  • Various SO answers are not applicable (already tried, see above) or suggest HACKs that don’t address the underlying problem (see below)
    • Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
    • Error: require() of ES modules is not supported when importing node-fetch
    • Error [ERR_REQUIRE_ESM]: require() of ES Module index.js not supported

Rejected solutions

What I consider a bad fix with other disadvantages and what I do not want to do:

  • Use a dynamic import(), because completely unnecessary here.
  • Rename our code files and change the file extensions
  • Downgrade the third-party library and use an old, unmaintained version of it.

Possible solutions

What I could imagine might work:

  • Change the build configuration of vite to emit import/ESM code instead of transpiling into require() code.
  • Fix vite?

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật