In my extraResources directory, I have a batch.js file that imports npm packages.
I exec()
a child process in the main electron process to run the batch.js file. The command looks like this: exec('node ${process.extraResources}/extra_resources/batch.js')
.
The batch.js file runs seamlessly in the Development mode, but when I build the app, batch.js file fails to load the npm packages.
I used electron-vite, which uses electron-builder, to initialize my Electron project.
My project structure:
src/extra_resources/batch.js
src/main/...
src/preload/...
src/renderer
My electron-builder.yml
appId: com.electron.app
productName: automation-electron
directories:
buildResources: build
files:
- '!**/.vscode/*'
- '!src/*'
- '!electron.vite.config.{js,ts,mjs,cjs}'
- '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
- '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
- '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'
asarUnpack:
- resources/**
win:
executableName: automation-electron
extraResources:
from: './src/extra_resources/'
to: extra_resources
filter:
- '**/*'
nsis:
artifactName: ${name}-${version}-setup.${ext}
shortcutName: ${productName}
uninstallDisplayName: ${productName}
createDesktopShortcut: always
mac:
entitlementsInherit: build/entitlements.mac.plist
extendInfo:
- NSCameraUsageDescription: Application requests access to the device's camera.
- NSMicrophoneUsageDescription: Application requests access to the device's microphone.
- NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder.
- NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
notarize: false
dmg:
artifactName: ${name}-${version}.${ext}
linux:
target:
- AppImage
- snap
- deb
maintainer: electronjs.org
category: Utility
appImage:
artifactName: ${name}-${version}.${ext}
npmRebuild: false
publish:
provider: generic
url: https://example.com/auto-updates