npm安装electron-edge-js出现的问题:Error: The edge module has not been pre-compiled for Electron version 15.

在尝试使用npm安装electron-edge-js时遇到了错误,提示该模块未针对Electron版本15.0.0预编译,需要自建版本。遵循GitHub上的指示,将 Electron 版本降至13后,问题成功解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 我在使用npm安装electron-edge-js时出现了以下问题:

Error: The edge module has not been pre-compiled for Electron version 15.0.0  . You must build a custom 
version of edge.node. Please refer to https://github.com/agracio/edge-js for building instructions.       
      at checkForPreCompiled (D:\projects\electron-vue\WiFiDockManager\node_modules\electron-edge-js\lib\edge.js:19:11)
      at Object.<anonymous> (D:\projects\electron-vue\WiFiDockManager\node_modules\electron-edge-js\lib\edge.js:31:18)
      at Module._compile (node:internal/modules/cjs/loader:1110:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1140:10)
      at Module.load (node:internal/modules/cjs/loader:982:32)
      at Module._load (node:internal/modules/cjs/loader:823:12)
      at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
      at Module.require (node:internal/modules/cjs/loader:1006:19)
      at require (node:internal/modules/cjs/helpers:93:18)
      at eval (webpack:///external_%22electron-edge-js%22?:1:18)

我的electron版本15.0.0、node版本14.17.1

根据GitHub - agracio/electron-edge-js: Run .NET and Node.js code in-process on Windows, MacOS, and Linux所述,是版本问题,

因此我将electron版本降到13,问题解决!

npm run build:portable > crm1@0.1.0 build:portable > vue-cli-service build --modern && electron-builder --win portable Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme All browser targets in the browserslist configuration have supported ES module. Therefore we don't build two separate bundles for differential loading. ⠇ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠋ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠼ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production... WARNING Compiled with 4 warnings 08:31:03 warning /js/chunk-vendors.1a0a0613.js is 2.28 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit. warning asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance. Assets: img/0001.bbc23d94.jpg (1.85 MiB) css/chunk-vendors.7d75f85b.css (306 KiB) js/chunk-vendors.1a0a0613.js (2.17 MiB) warning entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance. Entrypoints: app (2.6 MiB) css/chunk-vendors.7d75f85b.css js/chunk-vendors.1a0a0613.js css/app.e3c96718.css js/app.22cb88f3.js warning webpack performance recommendations: You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application. For more info visit https://webpack.js.org/guides/code-splitting/ File Size Gzipped dist\js\chunk-vendors.1a0a0613.js 2222.03 KiB 687.66 KiB dist\js\app.22cb88f3.js 130.49 KiB 17.91 KiB dist\workbox-5b385ed2.js 14.11 KiB 4.93 KiB dist\service-worker.js 1.20 KiB 0.76 KiB dist\css\chunk-vendors.7d75f85b.css 305.86 KiB 42.07 KiB dist\css\app.e3c96718.css 1.40 KiB 0.63 KiB Images and other types of assets omitted. Build at: 2025-07-03T00:31:03.176Z - Hash: 9fc57e568ce674b2 - Time: 40125ms DONE Build complete. The dist directory is ready to be deployed. INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html • electron-builder version=26.0.12 os=10.0.19041 • description is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • author is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • writing effective config file=dist\builder-effective-config.yaml • executing @electron/rebuild electronVersion=37.2.0 arch=x64 buildFromSource=false appDir=./ • installing native dependencies arch=x64 • completed installing native dependencies • packaging platform=win32 arch=x64 electron=37.2.0 appOutDir=dist\win-unpacked • downloading url=https://github.com/electron/electron/releases/download/v37.2.0/electron-v37.2.0-win32-x64.zip size=123 MB parts=8 • downloaded url=https://github.com/electron/electron/releases/download/v37.2.0/electron-v37.2.0-win32-x64.zip duration=2m44.871s • updating asar integrity executable resource executablePath=dist\win-unpacked\crm1.exe ⨯ Application entry file "index.js" in the "E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar" is corrupted: Error: "index.js" was not found in this archive failedTask=build stackTrace=Error: Application entry file "index.js" in the "E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar" is corrupted: Error: "index.js" was not found in this archive at error (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\asar\asarFileChecker.ts:6:12) at checkFileInArchive (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\asar\asarFileChecker.ts:15:11) at WinPackager.checkFileInPackage (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:638:25) at WinPackager.sanityCheckPackage (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:686:16) at WinPackager.doPack (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:358:5) at WinPackager.pack (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\platformPackager.ts:168:5) at Packager.doBuild (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\packager.ts:491:9) at executeFinally (E:\soft\finnal\yuanma2\crm1\node_modules\builder-util\src\promise.ts:12:14) at Packager.build (E:\soft\finnal\yuanma2\crm1\node_modules\app-builder-lib\src\packager.ts:425:31) at executeFinally (E:\soft\finnal\yuanma2\crm1\node_modules\builder-util\src\promise.ts:12:14)假如不管资源过大的问题
最新发布
07-04
<think>我们遇到了一个新的错误:Applicationentry file"index.js"in the... iscorrupted:Error:"index.js"was notfound inthis archive这个错误通常是因为electron-builder在打包时没有找到正确的入口文件。原因分析:1.默认情况下,electron-builder会寻找项目根目录下的`index.js`作为入口。2.但是,在Vue CLI项目中,我们通常将打包后的文件放在`dist`目录中,并且入口文件是`dist`目录中的文件。解决方案:我们需要在`package.json`中配置`main`字段,指向正确的入口文件。由于我们使用Vue构建,通常入口是`dist`目录下的`index.html`,但electron-builder需要的是一个JavaScript入口文件(主进程文件)。步骤:1.确保有一个主进程文件(比如`background.js`)在项目根目录(或者src目录)。2.在`package.json`中设置`main`字段为该文件(例如`"main": "background.js"`)。3.确保`background.js`中正确创建了Electron窗口并加载了`dist/index.html`。示例`background.js`文件内容:```javascriptconst{ app, BrowserWindow }= require('electron')const path= require('path') functioncreateWindow() {constwin =new BrowserWindow({width:800,height:600,webPreferences:{nodeIntegration:true}})//加载dist目录下的index.htmlwin.loadFile(path.join(__dirname,'dist','index.html'))} app.whenReady().then(createWindow) app.on('window-all-closed', ()=> {if(process.platform!== 'darwin') {app.quit()} })app.on('activate', ()=> {if(BrowserWindow.getAllWindows().length ===0) {createWindow()}}) ```4.修改`package.json`:```json{"name": "crm1","version":"0.1.0","main":"background.js",//指定主进程入口"author":"YourName","description":"Yourapplication description","scripts":{"build:portable":"vue-cli-service build--modern&& electron-builder--winportable"},//...其他依赖和配置} ```5.同时,我们需要确保Vue构建的输出目录是`dist`(默认就是),并且`background.js`中加载的路径正确。注意:在开发环境下,我们可能会使用不同的路径(比如使用webpack-dev-server时),所以我们可以根据环境变量来调整加载的URL。改进的`background.js`(考虑开发环境和生产环境):```javascriptconst{ app, BrowserWindow }= require('electron') constpath =require('path')//判断是否为开发环境constisDev= process.env.NODE_ENV ==='development' functioncreateWindow() {constwin =new BrowserWindow({width:800,height:600,webPreferences:{nodeIntegration: true}})if (isDev) {//开发环境:加载webpack-dev-server的地址win.loadURL('http://localhost:8080')win.webContents.openDevTools()} else{//生产环境:加载dist目录下的index.htmlwin.loadFile(path.join(__dirname, 'dist','index.html'))}} //...其余代码保持不变```6.在`package.json`中为开发环境设置环境变量(如果需要):我们可以使用`concurrently`和`wait-on`来同时启动前端开发服务器和Electron:```json"scripts":{"serve": "vue-cli-serviceserve","build":"vue-cli-service build","electron:dev": "concurrently \"npm runserve\"\"wait-on http://localhost:8080 &&electron .\""}```但是,在构建生产版本时,我们不需要开发服务器,所以`npmrun build:portable`会先构建Vue项目到`dist`目录,然后electron-builder打包。7.确保`background.js`文件在打包时被包含进去。electron-builder默认会打包除了`node_modules`以外的所有文件,但我们可以通过`build.files`配置来指定。在`package.json`的`build`字段中:```json"build":{"files": ["dist/**/*","background.js","package.json"]} ```8.如果使用asar打包,确保`background.js`在asar包外?通常不需要,因为我们的主进程文件必须在asar包外(因为asar包是在主进程加载的)。electron-builder会自动将主进程文件(即`main`字段指定的文件)放在asar包外。所以,按照上述步骤,应该可以解决找不到`index.js`的问题。但是,注意错误信息中说的是`index.js`,而我们使用的是`background.js`。所以,我们还需要确保electron-builder的配置中入口指向正确。另外,如果之前没有`main`字段,electron-builder默认会找`index.js`,所以设置`main`字段为`background.js`即可。现在,我们忽略资源过大的警告(按问题要求),专注于解决这个入口问题。重新运行构建命令: ```bashnpm runbuild:portable```如果问题仍然存在,请检查构建后的目录结构,确保`dist\win-unpacked\resources\app`目录下(或者asar包内)是否有`background.js`?实际上,主进程文件(`background.js`)应该是在asar包外,即`app.asar`的同级目录下。构建后的目录结构应该类似:``` dist/win-unpacked/crm1.exeresources/app.asarapp.asar.unpackedelectron.asarbackground.js#主进程文件应该在这里(与app.asar同级)?不对,实际上主进程文件会被打包进exe,但electron-builder会将主进程文件放在app.asar包外,即resources/app目录下(但作为入口文件,它会被放在根目录?)实际上,electron-builder打包后的结构:-可执行文件-resources/-app.asar(包含你的应用文件,除了主进程文件)- app.asar.unpacked(如果有需要解压的文件)-但是主进程文件(background.js)会被放在resources/app.asar的同级目录吗?不会。根据electron-builder的文档,主进程文件(即package.json中main字段指定的文件)会被打包进app.asar,除非你在build配置中指定将其解压出来。但是,主进程文件是必须放在根目录(即asar包外)的,因为它是启动入口。实际上,electron-builder会将主进程文件放在app.asar包外,即resources/app目录下,并且它会被单独放在resources目录下吗?不是。实际上,electron-builder将整个应用打包进resources/app.asar,而主进程文件也在其中。但是,为什么它启动时会找不到呢?原来,electron-builder在打包时会将主进程文件也放入app.asar中,但是启动时,Electron会从app.asar中加载主进程文件。所以,我们不需要将主进程文件放在asar包外。因此,问题可能在于:我们指定的主进程文件`background.js`在打包后没有包含在app.asar的根目录下?或者路径不对?我们可以通过以下方式调试:1.构建完成后,查看`dist\win-unpacked\resources\app.asar`的内容(使用asar工具解压):```bashnpx asar extractdist/win-unpacked/resources/app.asarapp-unpacked```2.检查解压后的目录中是否有`background.js`(在根目录下)。如果没有,说明electron-builder没有将`background.js`打包进去。我们需要检查`build.files`配置。在`package.json`的`build`中,我们配置了:```json"build":{"files": ["dist/**/*","background.js","package.json"]} ```这样配置应该会将`background.js`和`package.json`以及`dist`目录下的所有文件都打包进app.asar。所以,解压后应该能看到`background.js`在根目录。如果还是找不到,我们可以尝试在`background.js`中使用绝对路径加载`index.html`,确保路径正确。另外,注意在构建Vue项目后,`dist`目录下会有`index.html`,所以`background.js`中生产环境加载的路径应该是`path.join(__dirname, 'dist','index.html')`。但是,在打包后的app.asar中,目录结构是:app.asar|-dist/(包含Vue构建的所有文件)| -background.js| -package.json所以,在`background.js`中加载`index.html`的路径应该是`path.join(__dirname, 'dist','index.html')`,这是正确的。如果问题依旧,我们可以尝试在`background.js`中打印当前目录和文件路径,但生产环境不方便调试。所以,我们可以先确保开发环境能运行。开发环境运行: ```bashnpmrun electron:dev```如果开发环境正常,那么问题可能出在打包配置。另一种解决方案:使用electron-builder的配置项来指定额外的文件。但是,我们已经配置了`files`,所以应该没问题。最后,注意错误信息中的路径:`E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar`,它尝试在app.asar中找`index.js`,但我们配置的是`background.js`。所以,可能是因为package.json中的`main`字段没有生效?或者构建过程中package.json被修改了?我们可以在构建后检查打包进去的`package.json`内容:解压app.asar,看看里面的`package.json`的`main`字段是否是`background.js`。如果不是,说明在打包过程中,`package.json`被修改了?或者没有包含进去?因此,我们可以在`build`配置中强制指定入口文件:```json"build":{"files": ["dist/**/*","background.js","package.json"],"extraMetadata": {"main":"background.js"}} ```使用`extraMetadata`可以确保在打包时,app.asar中的`package.json`的`main`字段被设置为`background.js`。综上所述,我们进行以下步骤:1.创建`background.js`文件(内容如上,注意路径)。2.在`package.json`中设置`"main": "background.js"`。3.在`package.json`的`build`字段中配置`files`和`extraMetadata`(可选,但建议)。4.重新运行构建。如果问题仍然存在,请检查electron-builder版本,并尝试升级或降级。由于时间关系,我们直接给出修正后的配置,并忽略资源过大的问题(按题目要求)。修正后的`package.json`示例:```json{"name":"crm1","version": "0.1.0","main":"background.js","author":"YourName","description":"Yourapplication description","scripts": {"build:portable": "vue-cli-servicebuild --modern &&electron-builder --win portable"},"build": {"files":["dist/**/*","background.js","package.json"],"extraMetadata":{"main": "background.js"}},"dependencies":{// ...你的依赖},"devDependencies":{// ...你的开发依赖} }```重新运行构建命令: ```bashnpm runbuild:portable```期望结果:构建成功,生成便携式应用,并且可以运行。</think>你的构建失败是由于 Electron 打包过程中找不到入口文件 `index.js` 的问题。让我们专注于解决这个核心错误: ### 错误分析 ``` ⨯ Application entry file "index.js" in the "E:\soft\finnal\yuanma2\crm1\dist\win-unpacked\resources\app.asar" is corrupted: Error: "index.js" was not found in this archive ``` 这个错误表明 Electron 在打包后的应用存档(ASAR)中找不到预期的入口文件 `index.js`。常见原因有: 1. **入口文件配置错误** - Electron 找不到正确的入口点 2. **文件路径问题** - 构建输出路径与 Electron 预期路径不匹配 3. **ASAR 打包问题** - Electron 的 ASAR 打包机制未能包含关键文件 ### 解决方案 #### 1. 配置正确的 Electron 入口文件 在项目根目录创建 `background.js` 作为 Electron 主进程入口文件: ```javascript // background.js const { app, BrowserWindow } = require('electron') const path = require('path') function createWindow() { const mainWindow = new BrowserWindow({ width: 1200, height: 800, webPreferences: { nodeIntegration: true, contextIsolation: false } }) // 加载 Vue 构建后的应用 mainWindow.loadFile(path.join(__dirname, 'dist', 'index.html')) } app.whenReady().then(() => { createWindow() app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() }) ``` #### 2. 更新 package.json 配置 ```json { "name": "crm1", "version": "0.1.0", "main": "background.js", // 指定 Electron 入口文件 "author": "Your Name <your.email@example.com>", "description": "Your CRM Application", "scripts": { "build:portable": "vue-cli-service build --modern && electron-builder --win portable" }, "build": { "files": [ "dist/**/*", "background.js", "package.json" ], "directories": { "buildResources": "public" }, "asar": true, "win": { "target": "portable", "icon": "public/icon.ico" } } } ``` #### 3. 修改 vue.config.js 中的 Electron 配置 ```javascript // vue.config.js module.exports = { // ...其他配置保持不变... pluginOptions: { electronBuilder: { nodeIntegration: true, builderOptions: { appId: 'com.yourcompany.crm', productName: 'Your CRM', files: [ "dist/**/*", "background.js", "package.json" ], win: { target: "portable", icon: "public/icon.ico" }, asar: true } } } } ``` #### 4. 确保文件结构正确 构建后的文件结构应该是这样的: ``` dist/ index.html js/ css/ img/ background.js package.json ``` ### 修复步骤 1. **创建入口文件**: ```bash touch background.js ``` 将上面的代码复制到文件中 2. **更新配置文件**: - 更新 `package.json` 如上所示 - 更新 `vue.config.js` 如上所示 3. **清理缓存并重建**: ```bash rm -rf node_modules dist dist_electron npm install npm run build:portable ``` ### 关键配置说明 1. **`main` 字段**: ```json "main": "background.js" ``` 指定 Electron 的主进程入口文件 2. **`files` 配置**: ```json "files": [ "dist/**/*", "background.js", "package.json" ] ``` 明确告诉 Electron-builder 哪些文件需要包含在打包中 3. **加载路径**: ```javascript mainWindow.loadFile(path.join(__dirname, 'dist', 'index.html')) ``` 正确加载 Vue 构建后的入口 HTML 文件 ### 备用方案:使用 electron-forge 如果上述方案仍不工作,可以使用 electron-forge 作为替代方案: ```bash # 安装 electron-forge npm install --save-dev @electron-forge/cli npx electron-forge import # 更新 package.json 脚本 "build:portable": "vue-cli-service build --modern && electron-forge make --platform=win32 --targets=@electron-forge/maker-portable" # 运行构建 npm run build:portable ``` ### 预期结果 1. 成功构建便携式应用 2. 在 `dist_electron` 目录生成 `your-app-portable.exe` 3. 应用启动时显示 Vue 构建的界面 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值