由于serialport里面有一些原生编译的代码需要运行node-gyp提前build一下, 而node-gyp使用的node V8引擎版本与electron的V8引擎版本不同, 导致直接在node中可以require('serialport'),而在electron中却不行.
在electron中报错如下:

var SP = require('serialport')
C:\r\electron\macgetter\node_modules\bindings\bindings.js:83 Uncaught Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\r\electron\macgetter\node_modules\serialport\build\Release\serialport.node
    at Error (native)
    at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:168:20)
    at Object.Module._extensions..node (module.js:583:18)
    at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:168:20)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at bindings (C:\r\electron\macgetter\node_modules\bindings\bindings.js:76:44)module.(anonymous function) @ ELECTRON_ASAR.js:168Module._extensions..node @ module.js:583module.(anonymous function) @ ELECTRON_ASAR.js:168Module.load @ module.js:473tryModuleLoad @ module.js:432Module._load @ module.js:424Module.require @ module.js:483require @ internal/module.js:20bindings @ C:\r\electron\macgetter\node_modules\bindings\bindings.js:76(anonymous function) @ C:\r\electron\macgetter\node_modules\serialport\lib\bindings.js:3Module._compile @ module.js:556Module._extensions..js @ module.js:565Module.load @ module.js:473tryModuleLoad @ module.js:432Module._load @ module.js:424Module.require @ module.js:483require @ internal/module.js:20(anonymous function) @ C:\r\electron\macgetter\node_modules\serialport\lib\serialport.js:12Module._compile @ module.js:556Module._extensions..js @ module.js:565Module.load @ module.js:473tryModuleLoad @ module.js:432Module._load @ module.js:424Module.require @ module.js:483require @ internal/module.js:20(anonymous function) @ VM133:1

查找了一些相关文档, 如下:
electron使用serialport
Using node-serialport in an electron app

node-serialport的官方回答, 看下面voodootikigod的回答:
Tracking Electron support #538

electron的官方回答:
Using Native Node Modules

先实践着...

标签: none 阅读量: 1370

添加新评论