I'm trying to make an Electron application (https://electron.atom.io/) that reads data from my serial port. I'm new to web technologies in general, I know some javascript, but I'm a c++ guy.
So I pulled in their quick-start from github, ran
npm install && npm start
With this easily working I tried to install and run serialport with
npm install serialport
With that installed and running fine with a test file, I tried to combine the two and put require('serialport')
in the index.html file. With this in there I get this error:
Uncaught Error: The module '/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/serialport/build/Release/serialport.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 51. This version of Node.js requires NODE_MODULE_VERSION 53. Please try re-compiling or re-installing the module (for instance, using `npm rebuild` or`npm install`). at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20) at Object.Module._extensions..node (module.js:598:18) at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at bindings (/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/bindings/bindings.js:76:44) at Object.<anonymous> (/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/serialport/lib/bindings.js:3:35)
Any ideas how to fix it? I'm not using two different versions of Node, why am I getting this error.
System OS info:
Distributor ID: Ubuntu Description: Ubuntu 16.04.2 LTS Release: 16.04 Codename: xenial
The native Node modules are supported by Electron, but since Electron is very likely to use a different V8 version from the Node binary installed in your system, you have to manually specify the location of Electron's headers when building native modules.
To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.
You could remove your node_modules/ folder and then reinstall the dependencies from package. json. This would erase all installed packages in the current folder and only install the dependencies from package.
When this type of version mismatch occurs, you can either choose an electron distribution with the target Node version or rebuild the npm package. Since Electron's distribution has skipped Node v7.0.0 which is configured with NODE_MODULE_VERSION 51 (and jumped to v7.4.0), you would have to rebuild the serialport
package.
In your app's directory (where package.json is located at),
1. Install electron-rebuild
npm install --save-dev electron-rebuild
2. Rebuild
./node_modules/.bin/electron-rebuild
Or, even a better option - set environment variables from the first place.
# Electron's version. export npm_config_target=1.6.1 # The architecture of Electron, can be ia32 or x64. export npm_config_arch=x64 export 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
Take a look at the Electron's documentation page for using native Node modules. https://electron.atom.io/docs/tutorial/using-native-node-modules/
electron-rebuild
on postinstall
.Depending on what you're doing, you can use electron-rebuild to rebuild serialport
to the version of electron
you have installed.
To do so:
npm install --save-dev electron-rebuild $(npm bin)/electron-rebuild # Mac and Linux. .\node_modules\.bin\electron-rebuild.cmd # Windows.
Because I kept forgetting to do this after doing an npm install (and to help others that downloaded the project), I added the following two scripts to package.json
:
"scripts": { "start": "electron .", "postinstall": "electron-rebuild", "electron-rebuild": "electron-rebuild" },
The postinstall
will automatically run after doing a npm install
so after the typical install finishes you'll see a console log message with electron-rebuild
and it will automatically rebuild serialport
, and any other native library you have, to the electron
version. This means that you shouldn't even have to think about running electron-rebuild
going forward. 👍
To manually re-run electron-rebuild
just run it with npm run electron-rebuild
.
Easy-peezie, lemon-squeezie!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With