首页 > 解决方案 > 使用 spawn 方法从电子应用程序运行子进程的问题

问题描述

我试图找出一种使用电子打包器在打包的电子应用程序中运行节点子进程的方法。我的要求是在没有安装节点的机器上运行这个应用程序,所以我应该参考与电子应用程序捆绑的节点来运行子进程。我打包了应用程序并在没有安装节点的机器上执行它并且遇到了某些问题,我尝试通过在安装了节点的机器上使用未打包的应用程序来重新创建相同的问题,所以现在我意识到这不是问题没有节点安装。

我正在使用设置了环境变量 ELECTRON_RUN_AS_NODE 的 spawn 方法,使用 process.execPath 作为命令,但我面临以下问题。

注意:即使在安装了节点的机器上运行的未打包应用程序上,我也会遇到此错误。相同的代码在同一台机器上用 exec 方法用 'node' 命令代替 'process.execPath' 工作得很好。

    stdout: /Users/UserName/Desktop/proj/node_modules/@firebase/firestore/node_modules/grpc/src/grpc_extension.js:57
throw e;
^Error: 
dlopen(/Users/UserName/Desktop/proj
/name/node_modules/@firebase/firestore/node_modules/grpc/
src/node/extension_binary/node-v57-darwin-x64-unknown/grpc_node.node, 
1): Symbol not found: _GENERAL_NAME_free
Referenced from: 
/Users/UserName/Desktop/proj/
name/node_modules/@firebase/firestore/node_modules/grpc
/src/node/extension_binary/node-v57-darwin-x64-unknown/grpc_node.node
 Expected in: flat namespace
 in /Users/UserName/Desktop/proj/name/node_modules/@firebase/firestore/node_modules/grpc/src/node/extension_binary/node-v57-darwin-x64-unknown/grpc_node.node
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:172:20)
at Object.Module._extensions..node (module.js:671:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:172:20)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/UserName/Desktop/proj/name/node_modules/@firebase/firestore/node_modules/grpc/src/grpc_extension.js:32:13)
at Object.<anonymous> (/Users/UserName/Desktop/proj/name/node_modules/@firebase/firestore/node_modules/grpc/src/grpc_extension.js:63:3)

请在下面找到我的代码:

'use strict'

const fixPath = require('fix-path');
let functionName = () => {
    fixPath();           
    const child = childProcess.spawn(process.execPath, [path, 
    ....args], {
      // stdio: 'ignore',
      env: {
          ELECTRON_RUN_AS_NODE: 1
      }
});

child.on('error', (err) => {

});

child.stderr.on('data', function(data) {
    console.log('stdout: ' +data);
});

child.on('exit', (code, signal) => {
    console.log(code);
    console.log(signal);
});

child.unref();

}

标签: node.jselectronchild-processelectron-builderelectron-packager

解决方案


您可以使用 spawn 来生成子进程并使用电子环境变量。

child_process = spawn(command,args,{env: {"ELECTRON_RUN_AS_NODE":"1"}});

推荐阅读