【问题标题】:Execute a node.js script in packaged electron app在打包的电子应用程序中执行 node.js 脚本
【发布时间】:2017-10-14 08:04:33
【问题描述】:

在电子打包应用程序中,我正在尝试从 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:

Node API 可以执行 child_process.exec、child_process.spawn 和 child_process.execFile 等二进制文件,但仅支持 execFile 来执行 asar 存档中的二进制文件。

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

【问题讨论】:

  • 如果您使用 electron-builder 打包您的应用程序,您的 package.json 的 build 部分中有一个 extraResources 选项,您可以使用该选项将某些文件复制到 resources/app 目录中,在阿萨尔之外。如果您将应用程序与 node.js 二进制文件捆绑在一起,则可以使用子进程在额外资源目录中生成脚本。
  • @Bailey 非常感谢!!!正是我想要的

标签: node.js electron


【解决方案1】:

您不需要将 node 与电子捆绑在一起。 只需确保您的 '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 将使用捆绑节点,为其添加透明的 asar 支持并从 asar 存档运行脚本。

如果你cp.execFile('node'...你会使用外部节点,那不支持asar。

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 2019-01-11
    • 2018-12-11
    • 2020-09-15
    • 2020-08-10
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    相关资源
    最近更新 更多