If you go to https://github.com/maximegris/angular-electron you can see how to use native libraries at the bottom the page.
In electron.service.ts add the import:
import * as SerialPort from 'serialport';
Then inside the ElectronService class add:
serialPort: typeof SerialPort;
Then inside the constructor add to the if statement the following:
if(this.isElectron()){
[...]
this.serialPort = window.require('serialport');
}
Back in the home.components.ts file add the import
import {ElectronService} from '../../providers/electron.service';
Inject the service into the constructor:
constructor(private electron:ElectronService){...}
Finally, in the ngOnInit() function you can use the serial port like so:
this.electron.serialPort.list().then((ports:any)=>{
[...]
}).catch((err:any)=>{
[...]
});
Answer from Al55 on Stack OverflowIf you go to https://github.com/maximegris/angular-electron you can see how to use native libraries at the bottom the page.
In electron.service.ts add the import:
import * as SerialPort from 'serialport';
Then inside the ElectronService class add:
serialPort: typeof SerialPort;
Then inside the constructor add to the if statement the following:
if(this.isElectron()){
[...]
this.serialPort = window.require('serialport');
}
Back in the home.components.ts file add the import
import {ElectronService} from '../../providers/electron.service';
Inject the service into the constructor:
constructor(private electron:ElectronService){...}
Finally, in the ngOnInit() function you can use the serial port like so:
this.electron.serialPort.list().then((ports:any)=>{
[...]
}).catch((err:any)=>{
[...]
});
The angular-electron seed also install the npm modules in the /dist folder, so you will also need to run electron-rebuild on the serialport module in the dist folder to make it work.
As it is described in the angular-electron seed.
If you need to use NodeJS native libraries, you MUST add it manually in the file webpack.config.js in root folder
module.exports = {
"externals": {
"serialport": "require('serialport')"
}
}
in the package.json add:
"scripts": {
"postinstall": "electron-rebuild -f -w serialport"
}
and then run
npm start
Serialport Issues
Is it possible to use node-serialport?
node.js - using serialPort library with Angular - Stack Overflow
angular - Electron + Angular7 How to use serialport.list() in a Component - Stack Overflow
Videos
» npm install browser-serialport
» npm install serialport