gpt4 book ai didi

node.js - 在打包的 Electron 应用程序中执行node.js脚本

转载 作者:行者123 更新时间:2023-12-03 12:10:38 28 4
gpt4 key购买 nike

在 Electron 封装的应用程序中,我试图从node_modules依赖关系执行服务器文件。
在主要过程中,我正在尝试类似的方法:

var cp = require('child_process')
cp.execFile('node', path.join(__dirname, 'node_modules/my-module/server.js'))

从本地命令行启动应用程序时,我看到服务器已按预期启动,但打包为asar时却未启动。
实现该目标的正确方法是什么?

笔记:
我研究了 https://electron.atom.io/docs/tutorial/application-packaging/#executing-binaries-inside-asar-archive:

There are Node APIs that can execute binaries like child_process.exec, child_process.spawn and child_process.execFile, but only execFile is supported to execute binaries inside asar archive.



另外,看到了这样的答案:
Executing a script inside an ASAR archive这表示我需要对脚本进行 require-但是,我认为这是错误的。实际上,这是在同一进程(需要一次)中而不是在执行 execFile时生成此脚本的。

最佳答案

您不需要将 Node 与 Electron 捆绑在一起。
只要确保将您的“node_modules/my-module/server.js”打包为asar并使用

cp.fork(path.resolve(__dirname, 'node_modules/my-module/server.js'))
或者
cp.fork(require.resolve('my-module/server.js'))
它应该工作正常。
这样, Electron 将使用捆绑的 Node ,向其添加透明的asar支持,并从asar存档运行脚本。
如果您使用 cp.execFile('node'...,则将使用外部 Node ,该 Node 不支持asar。

关于node.js - 在打包的 Electron 应用程序中执行node.js脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43981150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com