1

I created an Electron project with Vite.

And I imported Electron from the file to be used in the Worker.

No Electron object was returned, only the Electron installation path.

How do I get an Electron object?

I saw the question below, but I didn't understand it.

node.js require returns a string instead of module object

  • file structure

    enter image description here

  • file contents

    src/main/index.ts

      import { app, shell, BrowserWindow, ipcMain } from 'electron'
      import { join } from 'path'
      import { electronApp, optimizer, is } from '@electron-toolkit/utils'
      import icon from '../../resources/icon.png?asset'
      import creatWorker from './worker?nodeWorker'
    
      function createWindow(): void {
      const mainWindow = new BrowserWindow({
          width: 900,
          height: 670,
          show: false,
          autoHideMenuBar: true,
          ...(process.platform === 'linux' ? { icon } : {}),
          webPreferences: {
          preload: join(__dirname, '../preload/index.js'),
          nodeIntegrationInWorker: true,
          contextIsolation: true,
          nodeIntegration: true,
          sandbox: true
          }
      })
    
      mainWindow.on('ready-to-show', () => {
          mainWindow.show()
      })
    
      mainWindow.webContents.setWindowOpenHandler((details) => {
          shell.openExternal(details.url)
          return { action: 'deny' }
      })
    
      if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
          mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
      } else {
          mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
      }
      }
    
      app.whenReady().then(() => {
      electronApp.setAppUserModelId('com.electron')
      app.on('browser-window-created', (_, window) => {
          optimizer.watchWindowShortcuts(window)
      })
    
      ipcMain.on('ping', () => console.log('pong'))
    
      createWindow()
      creatWorker({}).on('message', (message) => {
          console.log(`\nMessage from worker: ${message}`)
      })
    
      app.on('activate', function () {
          if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
      })
    
      app.on('window-all-closed', () => {
      if (process.platform !== 'darwin') {
          app.quit()
      }
      })
    

    src/main/worker.ts

    import test from 'electron'
    console.log(typeof test)
    // return 'string'
    console.log(test)
    // return '/Users/x/Documents/code/my_project/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron'
    
2
  • Try import * as test from 'electron'
    – Konrad
    Commented Jun 20 at 11:21
  • 1
    @Konrad The import result is that the default property and numeric properties from 0 to 104 are imported. The default property is a string that represents the path to Electron. Numeric properties from 0 to 104 are assigned one letter to each digit of the Electron path string above. And there was no Electron API object as expected...
    – sandwich
    Commented Jun 20 at 16:21

0

Browse other questions tagged or ask your own question.