question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ElectronBuilder and SerialPort

See original GitHub issue

Describe the bug

I can’t set data from a native module declared as external to a vue component property.

To Reproduce

I’m using a native module called serialport, for it to work it was necessary to declare as externals in the vue.config.js:

module.exports = {
    pluginOptions: {
        electronBuilder: {
            externals: ['serialport']
        }
    }
};

I followed these steps according to the electron-builder guidelines and can be found at this link: https://github.com/nklayman/vue-cli-plugin-electron-builder/blob/master/docs/guide/guide.md#native-modules-

SerialPort has a function to list the available serial ports and it returns a promise, I can print these ports on the console, but I can’t save them to a Vue component property, I believe it is because it is an external module compared to webpack.

Component code:

<template>
    <div>
        <div v-for="(port, index) in ports.length" :key="port + index">
            <div :id="'port' + index">Port: {‌{ port }}</div>
        </div>
    </div>
</template>

<script>
export default{
    name: 'SerialComm',
    data: () => ({
        ports: [],
    }),
    created() {
        SerialPort.list().then((ports) => {
            for (let i = 0; i < ports.length; i++) {
                console.log('Port', i, ports[i].comName)
                this.ports.push(ports[i].comName);
            }
            console.log('Promise this.ports', this.ports);
        });
        console.log('Created this.ports', this.ports);
    }
};
</script>

Console output:

image

Browser output:

image

Expected behavior

In the browser output, it printed “Port: 1” and “Port: 2”, where “Port:” is the text passed and the values “1” and “2” the value of the interpolated port property, it looks like it is printing the array index value + 1 instead of “COM4” and “COM19” as in console.

Environment (please complete the following information):

  • OS and version: Windows 10
  • node version: v10.16.2
  • npm version: 6.9.0
  • yarn version (if used): Not used
  • vue-cli-plugin-electron-builder version : 1.4.0
  • electron version: 5.0.10
  • other vue plugins used: serialport (7.1.5)
  • custom config for vcp-electron-builder: The standard was used
  • (if possible) link to your repo: Project has no repository yet

Additional context

This same module has other methods, but you need to create an object and from there everything works perfectly, as in the code below:

const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const portName = 'COM4';
 
const port = new SerialPort(portName, { baudRate: 115200 });
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));
 
port.on('open', () => {
    console.log('Serial port open');
    const msg = 'First Message';
    port.write(msg, function(err) {
        if (err) console.log('Error on write: ', err.message);
        else console.log('Message written: ', msg);
    });
});
port.on('error', (err) => { console.log('Error: ', error.message); });
parser.on('data', (data) => { console.log('Serial:', data); });

Console output:

image

I have other components that have a promise, a non-standard vue module that are working, and the only difference between them is the declaration of externals in the vue.config.js file as stated earlier, it seems that this snippet of code is running outside the instance of Vue.

Does anyone know how I could solve this problem?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
focussingcommented, Apr 14, 2021

@ana-marcia it’s already a while ago, what I did was to install the Xcode command line tools (macOS). Hope that helps 😃

1reaction
nklaymancommented, Aug 22, 2019

I found your issue! You were using v-for="(port, index) in ports.length", which would iterate over the length of the array, instead of the array itself. Replace this with v-for="(port, index) in ports".

As a side note, your code snippet had one of the most insane issues I have ever encountered. For some reason, the first { in {{ port }} wasn’t actually a {. It was some kind of weird character that looked exactly like a { but would make it so that {{ port }} was displayed in the rendered app. Wow. image image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node-serialport does not work in electron v14 or v15 #2313
Hi! After upgrading electron to version 14.0.0 in my project, electron-builder starts to break my builds. It took me about 3 hours to...
Read more >
Building Electron with Ionic2, serialport, and electron-builder
I'm trying to develop a project based on Polyonic. Polyonic itself is a kind of seed mashup of Electron and Ionic2. My project...
Read more >
EmergingTechnologyAdvisors/node-serialport - Gitter
Hi everyone, first time question asker here. created a vue cli 3 app. vue added. added the vue-cli-plugin-electron-builder plugin. in the main rendering ......
Read more >
@serialport/bindings | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Installing SerialPort
Installing SerialPort. Installation Instructions​. For most "standard" use cases (a supported Node.js on Mac, Linux, or Windows on a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found