[英]Electron - NPM Modules in Renderer
I'm having difficulty accessing lodash in an electron renderer. 我在访问电子渲染器中的lodash时遇到困难。 I'm new to Electron and understand that both the main process and the renderer (local html file) have access to node. 我是Electron的新手,并且了解主进程和渲染器(本地html文件)都可以访问节点。 I can require something native to node core such as fs
and it works - when I try to require lodash, it returns an empty object. 我可以要求某些诸如fs
节点核心本机,并且可以正常工作-当我尝试要求lodash时,它将返回一个空对象。 So far I have: 到目前为止,我有:
lodash
from npm
to the local project (using --save
) 从npm
安装lodash
到本地项目(使用--save
) electron-rebuild
运行electron-rebuild
I refresh the app and step through the debugger only to find that lodash
is an empty object. 我刷新应用程序,并逐步通过调试器,才发现lodash
是一个空对象。
Am I missing something? 我想念什么吗? Can you only run core node modules in a renderer? 您只能在渲染器中运行核心节点模块吗?
To use packages in the renderer, you simply have to load it as a dependency in your package.json file and require it by name. 要在渲染器中使用包,您只需要将其作为依赖项加载到package.json文件中,并按名称要求即可。 To make sure your dependencies are up to date: 为了确保您的依赖关系是最新的:
dependencies
object of your package.json
file. 将模块的最新版本添加到package.json
文件的dependencies
对象中。 node_modules/
folder 删除您的node_modules/
文件夹 npm install
on your project folder 在项目文件夹上运行npm install
You should now be able to simply do require('module_name');
现在,您应该可以简单地执行require('module_name');
in your renderer.js
file. 在您的renderer.js
文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.