geckosio / geckos.io

🦎 Real-time client/server communication over UDP using WebRTC and Node.js http://geckos.io
BSD 3-Clause "New" or "Revised" License
1.34k stars 84 forks source link

CommonJS require error with Vite and Electron on es6 module #281

Closed KaliaJS closed 7 months ago

KaliaJS commented 7 months ago

When I try to import geckos in the main.js file of electron, I get CommonJS error. It's strange because I can correctly import express and other libraries. That's why I think the problem comes from geckos.

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/computer/code/game/node_modules/@geckos.io/server/lib/index.js from /Users/computer/code/game/dist-electron/main.js not supported.
Instead change the require of index.js in /Users/computer/code/game/dist-electron/main.js to a dynamic import() which is available in all CommonJS modules.
// Main.js
import express from 'express'
import geckos from '@geckos.io/server'
// Package.json
"electron": "^28.2.0",
"vite": "^4.0.0",
"vite-electron-plugin": "^0.8.3",
"vite-plugin-electron-renderer": "^0.14.5",
NicoMeltz commented 7 months ago

The same for me !

yandeu commented 7 months ago

Because of node-datachannel@0.5.x only supporting esm, geckos v3 only supports esm as well.

See: https://github.com/geckosio/geckos.io?tab=readme-ov-file#mega-version-3-available

KaliaJS commented 7 months ago

I'm confused because I only use ESM too. Unless electronjs uses ComonJS? I'm a little confused. Is it possible to have more explanations to help me find the problem?

KaliaJS commented 7 months ago

I imagine it's more of a problem with vitejs. I close here.