node-serialport does not work with Electron - node.js

My task is to build a HTML5 desktop app, which works with serial port, because I have an arduino on the other side on COM-port.
I installed electron-prebuilt and serialport packages both (I specified them in package.json).
In my main web page I included main.js, and there I tried to require the serialport package, but I got the following error:
Uncaught Error: Cannot find module
'D:\Projects\arduino\HTMLPresentation\node_modules\serialport\build\serialport\v1.7.4\Release\node-v45-win32-x64\serialport.node'
I've read some QA topics about that problem (some abracadabras with node-gyp), but noting works for me: on both windows 8.1 and Ubuntu 15.04. I don't know how to adopt serialport package for electron.

My enviroment:
Vue.js: 2.6.10
Electron: 2.0.18
Node: 8.9.3
Platform: linux
If you are using Eletron with Vue.js you need to rebuild the serialport module doing something like this:
npm install electron-rebuild --save-dev
after this, you can add in your package.json > section "scripts", another line saying:
"rebuild": "electron-rebuild -f -w serialport"
You can rebuild the module with terminal command
npm run rebuild
Now the serialport module should be imported and will work as expected. Goog luck!

Use Electron-Rebuild: This executable rebuilds native io.js modules against the version of io.js that your Electron project is using. This allows you to use native io.js modules in Electron apps without your system version of io.js matching exactly.
npm install --save-dev electron-rebuild
# Every time you run "npm install", run this
./node_modules/.bin/electron-rebuild
# On Windows if you have trouble, try:
.\node_modules\.bin\electron-rebuild.cmd

This is how you would solve it
Install windows build tools in your computer by running this in your powershell with administrator access
npm install --global --production windows-build-tools
or npm --add-python-to-path='true' --debug install --global windows-build-tools
setx PYTHON "%USERPROFILE%\.windows-build-tools/python27/python.exe"
set PYTHON
set PYTHON helps you check if its set up correctly.
This is not enough install electron rebuild globally in your project files
npm install -g electron-prebuilt
npm install --save serialport
We have to rebuild serialport to work with electron. To do this, we need to >install electron-rebuild:
npm install --save-dev electron-rebuild
The last step is to run electron-rebuild, once again, just in the root >directory of the app:
./node_modules/.bin/electron-rebuild
import and use in your electron-vue project
<script>
const sp = require('serialport')
export default {
name: 'landing-page',
methods: {},
created () {
sp.list(function (error, ports) {
if (error) {
console.log(error)
} else {
console.log(ports)
}
})
}
}
</script>

Related

Unable to use leveldown package using Electron

When I try to build my applicaton using electron it crashes on leveldown library
Error: The module '/Users/macosx/Documents/Electron/node_modules/leveldown/build/Release/leveldown.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 64. This version of Node.js requires
NODE_MODULE_VERSION 67. Please try re-compiling or re-installing
I have tried
rm -rf node_modules/leveldown
npm install
npm rebuild leveldown--update-binary
npm uninstall leveldown
Also tried this
I had the same problem and nothing mentioned here worked for me. Here is what >worked for me:
Require all dependencies you need in the main.js file that is run by
electron. (this seemed to be the first important part for me) Run npm
i -D electron-rebuild to add the electron-rebuild package Remove the
node-modules folder, as well as the packages-lock.json file. Run npm i
to install all modules. Run ./node_modules/.bin/electron-rebuild to
rebuild everything It is very important to run
./node_modules/.bin/electron-rebuild directly after npm i otherwise it
did not work on my mac.
I struggled with this for a couple days. The trick is to use electron-rebuild to build the native node module, and to include the option node.__dirname = true in your webpack config, as the leveldown bindings.js depend on the __dirname global provided by Node.

Not able to use npm install to install angular-seed from github

These Were the instruction on github
'-----------------
angular2-seed
A simple starter project demonstrating the basic concepts of Angular 2.
Usage
Clone or fork this repository
Make sure you have node.js installed version 5+
Make sure you have NPM installed version 3+
WINDOWS ONLY run npm install -g webpack webpack-dev-server typescript to install global dependencies
run npm install to install dependencies
run npm start to fire up dev server
open browser to http://localhost:3000
if you want to use other port, open package.json file, then change port in --port 3000 script
'-------------
I get the error saying package.json file not found, but I have package.json file on the folder please help. please see the link error message to see the errors in cmd prompt
Error message:
run these commands:
git clone https://github.com/angular/angular2-seed.git your-proj-name
cd your-proj-name
npm install
npm install -g webpack webpack-dev-server typescript
npm start
open browser then navigate to localhost:3000
for beginner, I'm prefer Angular CLI https://github.com/angular/angular-cli

Node & Brunch & PIXI.js on Windows, module "querystring" not found

I'm having troubles setting up a PIXI project on my Windows machine.
It's very easy to reproduce:
npm install -g brunch
brunch new .
npm install --save-dev pixi.js
Then insert require("pixi.js") somewhere (e.g. initialize.js), and
npm run start
Open localhost:3333 and there's the following error:
Uncaught Error: Cannot find module 'querystring' from 'url/url.js'
at require (app.js:61)
at expanded (app.js:34)
at app.js:147
at url.js:104
at url.js:737
at initModule (app.js:42)
at require (app.js:59)
at expanded (app.js:34)
at app.js:147
at determineCrossOrigin.js:10
It works perfectly on a Linux machine (debian), but I always get this error on my Windows machine.
$ node -v
v6.9.5
$ npm -v
4.2.0
$ systeminfo | grep "OS"
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.14393 N/A Build 14393
The troubling module is pixi. If I install and require any other module, it works fine. It also doesn't work if I install the querystring explicitly (npm install querystring), although it's a built-in module.
Any ideas?
I've got it resolved by using bower. It's a workaround though.
npm install -g bower
bower install --save pixi.js
Then the PIXI object will be available globally out of the box. Just use it in your code.

electron sqlite3 Uncaught Error: Cannot find module

I have built an electron app in Mac OS, when I try to export the package for windows and try to run it throws me following error:
Error Screenshot
My app uses sqlite3, I installed sqlite using following command:
npm install sqlite3 --build-from-source
My NODE_PATH is also set to the node_modules folder, also tried following commands:
npm install --save-dev electron-rebuild
# Every time you run "npm install", run this
./node_modules/.bin/electron-rebuild
But still I am not able to run sqlite3 on windows with electron
sqlite3 relies on node-pre-gyp, so you need to use the workaround provided in electron-rebuild when building sqlite3 for Electron.

"Fatal error: Unable to find local grunt." when running "grunt" command

I uninstalled grunt with following command.
npm uninstall -g grunt
Then I again installed grunt with following command.
npm install -g grunt-cli
Visit following link:
https://npmjs.org/package/grunt-html
I want to use the above grunt plugin
But when I run the grunt command it gives me following error:
D:\nodeJS\node_modules\grunt-html>grunt
grunt-cli: The grunt command line interface. (v0.1.6)
Fatal error: Unable to find local grunt.
If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:
http://gruntjs.com/getting-started
All is explained quite nicely on gruntjs.com.
Note that installing grunt-cli does not install the grunt task runner!
The job of the grunt CLI is simple: run the version of grunt which has
been installed next to a Gruntfile. This allows multiple versions of
grunt to be installed on the same machine simultaneously.
So in your project folder, you will need to install (preferably) the latest grunt version:
npm install grunt --save-dev
Option --save-dev will add grunt as a dev-dependency to your package.json. This makes it easy to reinstall dependencies.
You have to install grunt in your project folder
create your package.json
$ npm init
install grunt for this project, this will be installed under node_modules/. --save-dev will add this module to devDependency in your package.json
$ npm install grunt --save-dev
then create gruntfile.js and run
$ grunt
I think you have to add grunt to your package.json file. See this link.
I had this issue on my Windows grunt because I installed the 32 bit version of Node on a 64 bit Windows OS. When I installed the 64bit version specifically, it started working.
I had the same issue today on windows 32 bit,with node 0.10.25, and grunt 0.4.5.
I followed dongho's answer, with just few extra steps.
here are the steps I used to solve the error:
1) create your package.json
$ npm init
2) install grunt for this project, this will be installed under node_modules/. --save-dev will add this module to devDependency in your package.json
$ npm install grunt --save-dev
3) then create gruntfile.js , with a sample code like this:
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
globals: {
jQuery: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint']
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['jshint']);
};
here, src/**/*.js and test/**/*.js should be the paths to actual JS files you are using in your project
4) run npm install grunt-contrib-jshint --save-dev
5) run npm install grunt-contrib-watch --save-dev
6) run $ grunt
Note: when you require common package like concat, uglify etc, you need to add those modules via npm install, just the way we installed jshint and watch in step 4 & 5
if you are a exists project, maybe should execute npm install.
guntjs getting started step 2.
This solved the issue for me. I mistakenly installed grunt using:
sudo npm install -g grunt --save-dev
and then ran the following command in the project folder:
npm install
This resulted in the error seen by the author of the question.
I then uninstalled grunt using:
sudo npm uninstall -g grunt
Deleted the node_modules folder. And reinstalled grunt using:
npm install grunt --save-dev
and running the following in the project folder:
npm install
For some odd reason when you global install grunt using -g and then uninstall it, the node_modules folder holds on to something that prevents grunt from being installed locally to the project folder.

Resources