electron 使用原生node 模块

来源:互联网 发布:淘宝定金尾款几天内 编辑:程序博客网 时间:2024/05/06 08:34

electron 使用原生node  模块

The native Node modules are supported by Electron, but since Electron is verylikely to use a different V8 version from the Node binary installed in yoursystem, you have to manually specify the location of Electron's headers whenbuilding native modules.

原生node模块都能用于electron ,但是因为使用了不同的v8内核的原因,需要重新编译才能使用

原生模块,主要指那些c/c++的写的扩展,例如sqllite,ffi这些模块。为了调用第三方DLL,addon太复杂,ffi 不错。

前置条件:

需要本地安装vs2015或Visual C++ Build Tools,  python2.7.11

cnpm install node-gyp -g

好了,必要条件满足了,就可看下面:


官网提供了三种方式:

1、Using npm

# Electron's version.export npm_config_target=1.6.2# The architecture of Electron, can be ia32 or x64.export npm_config_arch=x64export npm_config_target_arch=x64# Download headers for Electron.export npm_config_disturl=https://atom.io/download/electron# Tell node-pre-gyp that we are building for Electron.export npm_config_runtime=electron# Tell node-pre-gyp to build module from source code.export npm_config_build_from_source=true# Install all dependencies, and store cache to ~/.electron-gyp.HOME=~/.electron-gyp npm install
windows下export 换成 set

这种方式正常情况下是没问题的,苦逼的中国IT 人 遇到最大的问题是

https://atom.io/download/electron 国内访问经常超时,导致下载不成功。万能的TB 出现了, set npm_config_disturl=https://npm.taobao.org/mirrors/atom-shell换成这个还是会有些报错,主要是少几个其他无关紧要的文件,这个时候只能看出错提示哪里报错 注释哪里。2、Installing modules and rebuilding for Electron 安装模块并重新编译为指定版本使用
npm install --save-dev electron-rebuild
# On Windows if you have trouble, try:.\node_modules\.bin\electron-rebuild.cmd
遇到的问题跟上面一样,下载必要的文件超时,因为默认是连接到s3.amazonaws.com
慢到吐血,悲了个催的。处理方法同上,哪里报错注释哪里。
这种方式因为不好定义 下载源,我没有测试。

3、Manually building for Electron 手动编译原生模块为指定的electron 版本
cnpm install ffi --save //先安装模块,会自动安装依赖 ref
进入模块对应的目录分别编译
cd node_modules/ffi
  cd node_modules/ref
正常来说应该用这个命令
node-gyp rebuild --target=1.6.2 --arch=ia32 --target_arch=ia32 --dist-url=https://atom.io/download/electron --msvs_version=2015
网络的原因,你懂的 ,换成这个
node-gyp rebuild --target=1.6.2 --arch=ia32 --target_arch=ia32 --dist-url=https://npm.taobao.org/mirrors/atom-shell --msvs_version=2015

好吧,跟方案一样,少文件了,从出错的位置 手动定位到文件install.js 改改改
      /*if (res.statusCode !== 200) {
            done(new Error(res.statusCode + ' status code downloading checksum'))
            return
          }*/

 if (res.statusCode !== 200) {
              //done(new Error(res.statusCode + ' status code downloading 64-bit ' + release.name + '.lib'))
              return
            }
大概有3、4个地方,改改就能正常安装了,泪流满面啊。。。

后记:这个问题纠缠了我2、3天,叫天天天不应,stackoverflow也没有这种问题的答案
网络问题是阻碍人类进步的 绊脚石。。。何时才能国际接轨啊???
node.js很多模块都是开源的,不行就HACK 下。

Node.js 安装模块遇到 MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,
1) 安装 .NET Framework 2.0 SDK;2) 安装 Microsoft Visual Studio 2005;或 3) 如果将该组件安装到了其他位置,请将其位置添加到系统路径中。
 gyp ERR! stack Error
一般是没有指定 vs 版本 ,加上参数即可 --msvs_version=2015 以下是64位系统中的编译命令行

node-gyp rebuild --target=1.6.5 --arch=x64  --dist-url=https://npm.taobao.org/mirrors/atom-shell --msvs_version=2015

感谢 http://www.cnblogs.com/conorpai/p/6407010.html 给了我方向。

0 0
原创粉丝点击