如何在Electron中使用node_modules? [英] How to use node_modules within Electron?

查看:1214
本文介绍了如何在Electron中使用node_modules?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将电子与Angular2,Typescript和Electron结合使用我试图找出如何使用通过npm安装的节点模块软件包. 当前的问题是我必须指定像var module = require('./node_modules/xyz/lib/xyz.js')这样的模块的位置.但是然后电子没有找到xyz的依存关系,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不包含该模块作为依赖项,如果该模块在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆