node.js 如何在 Electron 中使用 node_modules?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30664111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use node_modules within Electron?
提问by chrisber
Using electron in combination with Angular2, Typescript and Electron I am try to find out how to use a node module package installed via npm.
The current problem is that I have to specify the location of the module like var module = require('./node_modules/xyz/lib/xyz.js'). But then electron does not find the dependencies of xyz, which are located within ./node_modules/xyz/node_modules/yyyand complains ./yyy.jscan not be found.
将电子与 Angular2、Typescript 和 Electron 结合使用,我试图找出如何使用通过 npm 安装的节点模块包。当前的问题是我必须指定模块的位置,如var module = require('./node_modules/xyz/lib/xyz.js'). 但是随后电子没有找到 xyz 的依赖项,它们位于内部./node_modules/xyz/node_modules/yyy并抱怨./yyy.js找不到。
The electron app structure
电子应用程序结构
dist
├── angular2.dev.js
├── config.js
├── index.html
├── main.js
├── node_modules
├── package.json
└── app.js
回答by Yan Foto
UPDATE:
更新:
A similar questionhas been asked and my answerwould most probably help you here:
If you don't append the path to your app node_modulesdirectory under your app root to the NODE_PATHvariable it is not going to work. So you need to do something like this:
如果您不将应用node_modules程序根目录下的应用程序目录的路径附加到NODE_PATH变量,它将无法工作。所以你需要做这样的事情:
export NODE_PATH=/PATH/TO/APP/node_modules
electron /PATH/TO/APP
When exporting NODE_PATHmake sure that you provide an absolute path.
导出时NODE_PATH,请确保提供绝对路径。
如果电子在
requirerequire正常情况下找不到模块,则表明您package.jsonpackage.json不包含模块作为依赖项even即使该模块已经在您的distdist目录下可用。So make sure that you are inside distdirectory and use
所以请确保您在dist目录中并使用
npm install --save xyz
note the --saveflag!
注意--save国旗!
回答by basarat
The current problem is that I have to specify the location of the module like var module = require('./node_modules/xyz/lib/xyz.js')
当前的问题是我必须指定模块的位置,如 var module = require('./node_modules/xyz/lib/xyz.js')
You should be able to do var module = require('xyz');If you have it locates in the relative path ./node_modules/ ....that you mentioned.
var module = require('xyz');如果您将它定位在./node_modules/ ....您提到的相对路径中,您应该能够做到。
回答by Harry Merzin
If you didn't include the modules in your package.json I found it easiest to just copy all of them to node_modules in your release. It's something like releases > ARCHITECTURE > resources > node_modules
如果您没有在 package.json 中包含模块,我发现将它们全部复制到版本中的 node_modules 是最简单的。有点像releases > ARCHITECTURE > resources > node_modules

