electron-log icon indicating copy to clipboard operation
electron-log copied to clipboard

Cannot use `electron-log/preload` in preload script

Open TheOneTheOnlyJJ opened this issue 1 year ago • 1 comments

Attempting to import electron-log/preload in my preload file throws the following error:

[2024-08-02 18:20:22.052] [error]                             WebContents#preload-error: {
  preloadPath: 'C:\\Users\\Andrei\\Desktop\\BlackBox\\out\\preload\\preload.js',
  error: 'Error: module not found: electron-log/preload\n' +
    '    at preloadRequire (node:electron/js2c/sandbox_bundle:2:82852)\n' +
    '    at <anonymous>:4:1\n' +
    '    at runPreloadScript (node:electron/js2c/sandbox_bundle:2:83516)\n' +
    '    at node:electron/js2c/sandbox_bundle:2:83813\n' +
    '    at node:electron/js2c/sandbox_bundle:2:83968\n' +
    '    at ___electron_webpack_init__ (node:electron/js2c/sandbox_bundle:2:83972)\n' +
    '    at node:electron/js2c/sandbox_bundle:2:84095',
  webContents: { id: 1, url: 'http://localhost:5173/' }
}

I am using electron-vite 2.3.0 as my build tool and electron 31.2.1.

TheOneTheOnlyJJ avatar Aug 02 '24 15:08 TheOneTheOnlyJJ

Preload code should be bundled together with all dependencies (except for a case when nodeIntegration is enabled).

megahertz avatar Aug 02 '24 19:08 megahertz

I ended up just using this in my preload script:

import { ipcRenderer } from "electron";
import { LogLevel, LogMessage } from "electron-log";

export const sendLogToMainProcess = (scope: string, level: LogLevel, message: string): void => {
  ipcRenderer.send("__ELECTRON_LOG__", {
    date: new Date(),
    scope: scope,
    level: level,
    data: [message],
    variables: { processType: "renderer" }
  } satisfies LogMessage);
};

TheOneTheOnlyJJ avatar Jun 16 '25 18:06 TheOneTheOnlyJJ