Skip to content

Instantly share code, notes, and snippets.

@jedthehumanoid
Last active June 11, 2018 19:47
Show Gist options
  • Save jedthehumanoid/a7f8278e0a37d259adca to your computer and use it in GitHub Desktop.
Save jedthehumanoid/a7f8278e0a37d259adca to your computer and use it in GitHub Desktop.
Build node serialport for electron

Building node-serialport for Electron on Windows

These instruction are for Windows, on my Linux installation(Fedora 21) everything works by just installing serialport with npm (I don't know why!).

These are the steps I did to build node serialport for electron (formerly atom-shell) for Windows. I am completely certain there is a better way to do it, but since I struggled with it for quite a while, here is a solution anyway.

You need some version of Visual studio installed, I have VS 2012 Express for Desktop.

There were a couple of issues that prevented a working build:

  • node-pre-gyp and passing disturl to npm for customized headers
  • electron version (or rather V8 version in electron)
  • Using node-gyp when build depends on node-pre-gyp

Instructions

Install io.js

Not needed for electron per se, but npm and maybe other tools needed is bundled with io.js

I used 1.8.1, 32 bit arch

Unpack electron

Note: I used electron 0.24.0, since never versions came with v8 newer than used in latest io.js, which gave me toolchain mismatch when building. (I don't remember exact error, but something about NAN, serialport_win.cpp, V8 and 4 arguments in function signature)

Again I used 32 bit arch, I guess matching with same io.js arch results in minimum grievance.

Install serialport with npm

Commands executed in io.js command shell

cd **your electron installation**/resources/default_app
npm install serialport

This results in succesful build, but against wrong headers, which gives Uncaught Error: Module did not self-register.

Get correct headers

This command fails, but it's ok since I just used this command to download correct headers. There is surely a better way to download them though.

cd node_modules/serialport
node-gyp rebuild --target=0.24.0 --dist-url=https://atom.io/download/atom-shell

Note: This fails with Undefined variable module_name in binding.gyp while trying to load binding.

Note: just installing directly with correct headers (i.e. passing dist-url and target to npm) does not work. This adds node-pre-gyp to the mix, and node-pre-gyp doesn't know what to do with Electrons versioning scheme.
Results in: node-pre-gyp ERR! stack Error: Unsupported target version

Move headers

Move/rename/copy electron-headers(0.24.0) to 1.8.1 (io.js version)

cd **your home directory**/.node-gyp
mv 1.8.1 iojs_headers
mv 0.24.0 1.8.1
cd 1.8.1
mv ia32/node.lib ia32/iojs.lib
mv x64/node.lib x64/iojs.lib

Note I'm just guessing here, I really have no knowledge about node/iojs/electron libraries and headers.

Install serialport with npm again (with correct headers)

This time this will build correct version, since headers are correct

cd **your electron installation**/resources/default_app
npm install serialport

Have fun!

Again, this works for me, your mileage may vary

@mstllc
Copy link

mstllc commented May 6, 2015

Appreciate the tips, got me up and running!

@jedthehumanoid
Copy link
Author

Glad it helps someone!

Copy link

ghost commented May 14, 2015

This helped me out tremendously. Cheers!

@ddm
Copy link

ddm commented Jul 16, 2015

If anybody stumbles upon this while trying to use node-serialport with electron: there is a temporary solution in the form of the serialport-electron npm package.
Hopefully, in the long run, node-pre-gyp will support electron OOTB.

@AshMartian
Copy link

@ddm I am having this issue even with serialport-electron. I have tried electron-rebuild too with still the same result. Having successfull compiles but the error message It looks like serialport didn't compile properly. This is a common problem and its fix is well documented here https://github.com/voodootikigod/node-serialport#to-install won't go away. I installed io.js but don't see the version in ~/.node-gyp, I think my system might be tainted with node-pre-gyp but have no clue how to uninstall it.

@rafaelcorreiapoli
Copy link

Hi, with this, can I access serial ports on client or only on server?

@ddm
Copy link

ddm commented Sep 9, 2015

@blandman Could you post the error message you get when typing npm install serialport-electron?

Also could you post some info on you environment: OS, compiler, libs etc. In other word, anything that could be used to reproduce the bug...

@jedthehumanoid
Copy link
Author

I'm guessing this will be less of a issue, now that io.js is merged and electron has switched back to node.

@ruipimentel
Copy link

I'm having a lot of trouble trying to get serialport to run on Windows 7 64 bit (doesn't have a 32 bit version at hand to test it, though), and since io.js no longer exists, I'm unable to reproduce the steps on this page without a little adapting... Even then, despite getting full compilation, while requiring (require("serialport")) inside my Electron app I get this exception:

Error: The system cannot find message text for message number 0x%1 in the message file %2. C:\...\hello.node

Is anyone able to fix this, or at least point me in the right direction? :s
Thanks in advance!!! :)

@timfish
Copy link

timfish commented Mar 10, 2016

node-serialport now appears to work well on electron if you use electron-rebuild. You will have to use the preGypFix option to copy the module after build as its trying to pick up from the wrong path...

@andresfuseau
Copy link

Hi.. I did normal install but get:
App threw an error when running Error: Error en una rutina de inicialización de biblioteca de vínculos dinámicos (DLL).
?\C:\Program Files\nodejs\app\node_modules\serialport\build\Release\serialport.node
at Error (native)
at process.module.(anonymous function) as dlopen
at Object.Module._extensions..node (module.js:568:18)
at Object.module.(anonymous function) as .node
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at bindings (C:\Program Files\nodejs\app\node_modules\serialport\node_modules\bindings\bindings.js:76:44)

Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment