Skip to content

Comments

Add Electron desktop app with .cjs main file to fix ES module require error#105

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-js-require-error
Draft

Add Electron desktop app with .cjs main file to fix ES module require error#105
Copilot wants to merge 3 commits intomainfrom
copilot/fix-js-require-error

Conversation

Copy link
Contributor

Copilot AI commented Feb 19, 2026

Packaging the app as an Electron desktop exe fails at startup with ReferenceError: require is not defined in ES module scope because the Electron main process file uses require() but gets treated as ESM when package.json contains "type": "module".

Root cause

Node.js treats .js files as ES modules when the nearest package.json has "type": "module". The .cjs extension forces CommonJS regardless:

// main.cjs — always CommonJS, immune to "type": "module"
const { app, BrowserWindow } = require('electron');

Changes

  • apps/desktop/main.cjs — Electron main process using .cjs extension to guarantee CommonJS resolution
  • apps/desktop/preload.cjs — Preload script with contextIsolation: true, nodeIntegration: false
  • apps/desktop/package.json — electron-builder config, "main": "main.cjs", no "type": "module". Build script is package (not build) to avoid running during turbo build
  • .github/workflows/build-desktop.yml — Multi-platform build workflow (win/mac/linux), triggers on tags and workflow_dispatch
  • .gitignore — Exclude apps/desktop/dist/

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 19, 2026 01:03
The root cause was using a .js extension for the Electron main process file
when package.json contains "type": "module". Node.js treats .js files as
ES modules in that case, making require() unavailable.

Fix: Use .cjs extension for main.cjs and preload.cjs, which forces CommonJS
mode regardless of any "type": "module" setting in package.json.

Co-authored-by: Godzilla675 <131464726+Godzilla675@users.noreply.github.com>
Co-authored-by: Godzilla675 <131464726+Godzilla675@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix JavaScript error for ES module scope Add Electron desktop app with .cjs main file to fix ES module require error Feb 19, 2026
Copilot AI requested a review from Godzilla675 February 19, 2026 01:07
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

Successfully merging this pull request may close these issues.

2 participants