Electron打包生成的.exe安装后缺失async模块文件导致运行时错误的问题求助
Electron打包生成的.exe安装后缺失async模块文件导致运行时错误的问题求助
大家好,我最近在折腾一个Electron应用,用到的版本如下:
- electron: 22.0.0
- electron-builder: 23.6.0
- async npm包: 3.2.4
下面是我的各项配置和构建流程,麻烦帮忙看看哪里出了问题:
electron-builder配置文件(electron.build.cjs)
module.exports = { appId: 'com.electron.balbdldldld', productName: 'Hblalalala', files: ['./dist'], directories: { buildResources: 'resources', output: 'release', }, asarUnpack: '**\\*.{node,dll,exe}', extraResources: ['build/', 'resources/'], win: { target: ['nsis'], sign: 'scripts/sign.js', signingHashAlgorithms: ['sha256'], }, nsis: { guid: 'bbbbbbb', include: 'build/installer.nsh', artifactName: 'blalflflvl.${ext}', runAfterFinish: false, createStartMenuShortcut: false, createDesktopShortcut: false, }, };
渲染进程Vite配置(vite.config.ts)
import path from 'path'; import react from '@vitejs/plugin-react'; import { defineConfig } from 'vite'; import tsconfigPaths from 'vite-tsconfig-paths'; import sassDts from 'vite-plugin-sass-dts'; import { version } from './package.json'; export default defineConfig(() => ({ root: path.join(__dirname, 'app', 'renderer'), server: { port: 4200, open: true, }, base: './', build: { outDir: path.join(__dirname, 'dist') }, plugins: [react(), tsconfigPaths(), sassDts()], resolve: { alias: { '@/styles': path.join(__dirname, 'app', 'renderer', 'styles') } }, define: { __PACKAGE_VERSION__: JSON.stringify(version), }, }));
主进程Webpack配置(webpack/webpack.config.prod.ts)
import path from 'path'; import webpack from 'webpack'; import nodeExternals from 'webpack-node-externals'; import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; import TerserPlugin from 'terser-webpack-plugin'; /** * The function returns the OS target for compilation * @returns the OS target */ const getOsTarget = () => { if (process.platform === 'win32') { return 'windows'; } if (process.platform === 'darwin') { return 'darwin'; } return null; }; const osTarget = getOsTarget(); if (osTarget === null) { console.log('Failed to build, unsupported platform'); process.exit(1); } const rootPath = path.join(__dirname, '..'); const applicationCodePath = path.join(rootPath, 'app'); const distPath = path.join(rootPath, 'dist'); const nodeModulesPath = path.join(rootPath, 'node_modules'); const configuration: webpack.Configuration = { mode: 'production', plugins: [ new webpack.EnvironmentPlugin({ NODE_ENV: 'production', }), new webpack.NormalModuleReplacementPlugin(/\-\[(darwin|windows)\]./, (resource) => { resource.request = resource.request.replace(/(darwin|windows)/, osTarget); }), ], target: 'electron-main', entry: { main: path.join(applicationCodePath, 'main', 'main.ts'), preload: path.join(applicationCodePath, 'preload', 'main.ts'), }, externals: [nodeExternals({ modulesDir: path.join(rootPath, 'node_modules') })], output: { path: distPath, filename: '[name].js', library: { type: 'umd' }, }, module: { rules: [ { test: /\.ts$/, exclude: /node_modules/, use: [ { loader: 'ts-loader', options: { configFile: path.join(rootPath, 'tsconfig.build.json'), transpileOnly: true, }, }, ], }, ], }, resolve: { extensions: ['.js', '.json', '.ts'], modules: [applicationCodePath, nodeModulesPath], plugins: [ new TsconfigPathsPlugin({ configFile: path.join(rootPath, 'tsconfig.build.json'), extensions: ['.ts'], }), ], }, optimization: { minimizer: [ new TerserPlugin({ parallel: true, }), ], }, node: { __dirname: false, __filename: false, }, }; export default configuration;
我的构建步骤
- 构建渲染进程:
vite build -c ./vite.config.ts - 构建主进程:
webpack --config ./webpack/webpack.config.prod.ts - 打包Electron应用:
electron-builder --config ./electron.build.cjs
构建全程没有报错,而且我检查release/win-unpacked/resources/app.asar时,用npx asar unpack app.asar app.unpack解压后,能看到./app.unpack/node_modules/async/apply.js确实存在。但当我用生成的.exe安装应用后,运行时直接抛出了错误:
Uncaught exception:
Error: Cannot find module './apply'
Require Stack:
"........./resources/app.asar/node_modules/async/index.js"
我再解压安装后的app.asar,发现里面真的没有apply.js文件。有没有大佬能帮我分析下,为什么安装后的包会缺失这个文件?我必须让这个文件正常存在才能让应用跑起来。
备注:内容来源于stack exchange,提问作者some one
相关产品推荐
相关产品推荐

