如何在 Electron 中使用 node_modules? [英] How to use node_modules within Electron?
问题描述
将 electron 与 Angular2、Typescript 和 Electron 结合使用我试图了解如何使用通过 npm 安装的节点模块包.当前的问题是我必须指定模块的位置,例如 var module = require('./node_modules/xyz/lib/xyz.js')
.但是随后 electron 没有找到 xyz 的依赖,它们位于 ./node_modules/xyz/node_modules/yyy
并抱怨 ./yyy.js
找不到.
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/yyy
and complains ./yyy.js
can not be found.
电子应用结构
dist
├── angular2.dev.js
├── config.js
├── index.html
├── main.js
├── node_modules
├── package.json
└── app.js
推荐答案
更新:
A similar question has been asked and my answer would most probably help you here:
如果您不将应用程序根目录下的应用程序 node_modules
目录的路径附加到 NODE_PATH
变量,它将无法工作.所以你需要做这样的事情:
If you don't append the path to your app node_modules
directory under your app root to the NODE_PATH
variable it is not going to work. So you need to do something like this:
export NODE_PATH=/PATH/TO/APP/node_modules
electron /PATH/TO/APP
在导出 NODE_PATH
时,请确保提供绝对路径.
When exporting NODE_PATH
make sure that you provide an absolute path.
如果电子在您正常
require
时找不到模块,则表明您的 package.json
不包含模块作为依赖项 even 如果该模块已经在您的 dist
目录下可用.
If electron cannot find modules when you
require
them normally, it's a sign that your package.json
doesn't contain the modules as dependency even if the module is already available under your dist
directory.
所以请确保您在 dist
目录中并使用
So make sure that you are inside dist
directory and use
npm install --save xyz
注意 --save
标志!
note the --save
flag!
这篇关于如何在 Electron 中使用 node_modules?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!