Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

javascript - 在电子工进程中不能要求使用node_modules(Cannot require node_modules in electron worker process)

Question(题)

I'm trying to use web workers in electron.(我正在尝试在电子领域使用网络工作者。)

So far I'm able to instanciate the worker process from the renderer process, but when I try to do a require('some_module') in the worker process the process crashes with the error.(到目前为止,我能够从渲染器进程实例化工作进程,但是当我尝试在工作进程中执行require('some_module') ,该进程将因错误而崩溃。)

Cannot find module 'some_module' .(找不到模块'some_module' 。)

The cjs loader cannot find my module apparently.(cjs加载器显然找不到我的模块。)

But when I make the same require call from the renderer process, I'm able to require the module.(但是,当我从渲染器过程中进行相同的require调用时,我可以require该模块。)

I've followed all the steps mentioned here .(我已按照此处提到的所有步骤进行操作。)

Also I've set the option nodeIntegrationInWorker: true and I can make require calls to node inbuilt modules like fs with no problems.(另外,我还设置了选项nodeIntegrationInWorker: true并且可以nodeIntegrationInWorker: true require对诸如fs类的节点内置模块require调用。)

A few observations(一些观察)

  1. __dirname in the rendered process resolves to(呈现过程中的__dirname解析为)

    root/node_modules/electron/dist/resources/electron.asar/renderer(根目录/node_modules/electron/dist/resources/electron.asar/renderer)

    and in the worker process resolves to(并在工作程序中解决)

    root/node_modules/electron/dist/resources/electron.asar/worker(根目录/node_modules/electron/dist/resources/electron.asar/worker)

    as far as I've done the reading the require function should be able to find my module in the node_modules dir which is parent to both the renderer and worker dir(就我所做的阅读而言,require函数应该能够在node_modules目录中找到我的模块,该目录是渲染器目录和worker目录的父目录)

  2. A quick look at the process global in the worker reveals that process.type is equals worker while process.argv[1] is equals --type=renderer which I find strange.(快速查看worker process全局process ,发现process.type等于workerprocess.argv[1]等于--type=renderer ,我觉得很奇怪。)


Meta : Electron version = "4.0.0", platform = "win32", arch = "x64", node version = "v10.11.0"(Meta电子版本=“ 4.0.0”,平台=“ win32”,arch =“ x64”,节点版本=“ v10.11.0”)

Any help in this regard would be appreciated.(在这方面的任何帮助将不胜感激。)

  ask by Nishkal Kashyap translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok.(好。)

As a workaround, I use this.(作为解决方法,我使用它。)
    const paths = [
        path.join(process.resourcesPath, 'app.asar', 'node_modules'),
        path.join(process.resourcesPath, 'app', 'node_modules'),//when asar is disabled
        process.resourcesPath.replace(/electron[\/]dist[\/]resources/g, '')
    ];

    paths.map((path) => {
        global.require.main.paths.push(path);
    });

The above snippet manually adds the paths node looks to resolve the required module.(上面的代码片段手动添加了路径节点,以解决所需的模块。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...