简体   繁体   English

电子-渲染器中的NPM模块

[英]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: 到目前为止,我有:

  • installed lodash from npm to the local project (using --save ) npm安装lodash到本地项目(使用--save
  • run 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: 为了确保您的依赖关系是最新的:

  • add the module's latest version to the dependencies object of your package.json file. 将模块的最新版本添加到package.json文件的dependencies对象中。
  • delete your node_modules/ folder 删除您的node_modules/文件夹
  • run 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM