electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.5k stars 520 forks source link

webpack-typescript template can't resolve native node modules in webpack bundling #3495

Open selganor74 opened 9 months ago

selganor74 commented 9 months ago

Pre-flight checklist

Electron Forge version

7.2.0

Electron version

28.2.2

Operating system

Windows 10 22H2

Last known working Electron Forge version

No response

Expected behavior

Native node modules imported in preload.ts should be resolved correctly by webpack.

Actual behavior

"electron-forge start" fails at the webpack bundling, with this error:

√ Checking your system √ Locating application √ Loading configuration √ Preparing native dependencies [2s] √ Running generateAssets hook √ [plugin-webpack] Compiling main process code [2s] √ [plugin-webpack] Compiling main process code [2s] √ [plugin-webpack] Compiling main process code [2s] × [plugin-webpack] Launching dev servers for renderer process code › Compilation errors in the preload: group_0: asset main_window/preload.js 562 KiB [emitted] (name: main_window) runtime modules 26.…

An unhandled rejection has occurred inside Forge: Error: Compilation errors in the preload: group_0: asset main_window/preload.js 562 KiB [emitted] (name: main_window) runtime modules 26.4 KiB 12 modules modules by path ./node_modules/ 163 KiB modules by path ./node_modules/webpack-dev-server/client/ 71.8 KiB 16 modules modules by path ./node_modules/webpack/hot/*.js 5.18 KiB ./node_modules/webpack/hot/dev-server.js 1.94 KiB [built] [code generated] ./node_modules/webpack/hot/log.js 1.74 KiB [built] [code generated]

Steps to reproduce

init a new electron-forge app with:

npm init electron-app@latest my-app -- --template=webpack-typescript

cd my-app

add this code in src/preload.ts

import * as fs from "fs";

const c = fs.readdirSync("C:\\");
console.log("dir", c);

npm start

Additional information

The same test made using vite-typescript template, works as expected. For the code to work in electron, the main BrowserWindow must be created with webPreferences.sandbox: false.

JoueBien commented 9 months ago

From memory I resolved this by: setting target: "web", in webpack.renderer.config.ts

anovi commented 8 months ago

Same problem here. Webpack can't resolve native node modules in preload.js. Node integration enabled. Has someone found any solution?