Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use serialport in electron, I get error about "indexof" #1789

Closed
wiener0zyj opened this issue Jan 26, 2019 · 14 comments
Closed

use serialport in electron, I get error about "indexof" #1789

wiener0zyj opened this issue Jan 26, 2019 · 14 comments

Comments

@wiener0zyj
Copy link

Summary of Problem

(Please answer all 3)

  • What are you trying to do?
    I'm tring to use serialport in electron with react, after i webpack my codes and run npm start, I get this:

### Uncaught TypeError: Cannot read property 'indexOf' of undefined

please help take a look at it.

  • What happens?
    error happen when running:
Uncaught TypeError: Cannot read property 'indexOf' of undefined
    at Function.t.getFileName (bundle_Home.js:6)
    at t (bundle_Home.js:6)
    at Object.<anonymous> (bundle_Home.js:20)
    at n (bundle_Home.js:1)
    at Object.<anonymous> (bundle_Home.js:20)
    at n (bundle_Home.js:1)
    at Object.<anonymous> (bundle_Home.js:20)
    at n (bundle_Home.js:1)
    at Object.<anonymous> (bundle_Home.js:20)
    at n (bundle_Home.js:1)
  • What should have happened?
    I think it should works well

Code to Reproduce the Issue

import React from 'react';
import {render} from 'react-dom';
import 'whatwg-fetch';
import SerialPort from 'serialport';

export default class Home extends React.Component{
    constructor(props){
        super(props);

        this.initSerialPort = this.initSerialPort.bind(this);
    }

    componentDidMount(){
        this.initSerialPort();
    }

    initSerialPort(){
        SerialPort.list(function (err, ports) {
            ports.forEach(function(port) {
              console.log(port.comName);
              console.log(port.pnpId);
              console.log(port.manufacturer);
            });
        });
    }

    render(){
        return (
            <div className="home">
                <span>Hello World</span>
            </div>
        );
    }
}

render(<Home/>, document.getElementById("home"));
const serialport = new SerialPort(path)

// Code

Versions, Operating System and Hardware

  • SerialPort@?

  • Node.js v?
    v10.15.0

  • Windows? Linux? Mac?
    Win10

  • Hardware and chipset? (Prolific/FTDI/Other)

@wiener0zyj
Copy link
Author

could anyone help me to take a look?

@kfatehi
Copy link

kfatehi commented Feb 3, 2019

I have seen this error too. The root cause may be related to this issue kusti8/proton-native#196 .. I wonder if porting the native library to https://github.com/charto/nbind would resolve issues like these.

@reconbot
Copy link
Member

reconbot commented Feb 4, 2019

N-Api will help with these issues, someone is experimenting with it right now, come this April when we drop node 6 support we’ll be able to switch.

As I understand it Nbind won’t work because we have to be use c++ system calls

@reconbot
Copy link
Member

reconbot commented Feb 4, 2019

Also I don’t think the two of you have similar issues.

@Nishkalkashyap
Copy link

I'm having exactly the same issue. The code runs perfectly in the development environment in electron application. But throws the same error in production. I tested the latest version of serialport@7.1.4 with latest major releases of electron 2.x.x, 3.x.x and 4.x.x. Throws the same error all the time. @wiener0zyj were you able to find a solution to this?

@Nishkalkashyap
Copy link

Ok. I think i found out what was causing the problem for me. I'm using webpack to bundle certain modules, was relying on webpack-node-extarnals package to ignore serialport and not build it. But as I found out it was bundling serialport for me. My bad. Silly mistake. :(

@wiener0zyj
Copy link
Author

@Nishkalkashyap thanks very much! I have solved the problem, only electron 1.8.8 could support serialport.io. all the other versions such as 2.xx 3.xx and 4.xx could not work at all.

@reconbot
Copy link
Member

reconbot commented Feb 27, 2019 via email

@Nishkalkashyap
Copy link

Nishkalkashyap commented Feb 27, 2019

Well. As I said earlier, serialport sure does works for me with electron. At least the version v4.0.5 of electron that I use.

To reiterate, my only problem was that I was bundling serialport with webpack, which fails. As soon as I add serialport as external module in webpack, everything works fine.

@dragongears
Copy link

I had the same issue with serialport in my Electron app when I tried to update from serialport 6.2.2 to 7.1.4. Reverting back to 6.2.2 solved my problem.

@agmangas
Copy link

I also found myself dealing with this. I'll leave the webpack configuration that worked for me for other users for future reference:

{
  externals:  {
    serialport: 'commonjs serialport'
  }
}

The serialport section in the output bundle for this configuration would look like this:

/***/ "serialport":
/*!*****************************!*\
  !*** external "serialport" ***!
  \*****************************/
/*! no static exports found */
/***/ (function(module, exports) {

eval("module.exports = require(\"serialport\");\n\n//# sourceURL=webpack:///external_%22serialport%22?");

This means that when the app requires serialport it will end up resolving to the one located in the node_modules in the parent folder of the webpack output directory.

@stale
Copy link

stale bot commented Jun 15, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week no further activity occurs. Feel free continue the discussion or ask for a never-stale label to keep it open. If this is a support issue, consider sharing it on stack overflow to get more eyes on your problem.

@stale stale bot added the stale-issue label Jun 15, 2019
@stale stale bot closed this as completed Jun 22, 2019
@s00500
Copy link

s00500 commented Jul 8, 2019

I was using vue-cli-plugin-electron-builder and had the same problem, the solution in my case was to mark serialport as external for the builder

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      externals: ['serialport']
    }
  }
}

See here: electron-userland/electron-builder#3696

@thrazu
Copy link

thrazu commented Nov 20, 2019

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

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!

@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

8 participants