node-serialport: TypeError: Third argument must be a function
Summary of Problem
(Please answer all 3)
- What are you trying to do?: Open a connection to a serial port device via the serialport constructor on Windows 10, electron 11.1, bindings 9.0.4.
- What happens? When attempting to establish a connection to a serialport, it throws an error saying third argument must be a function.
-
at internal/util.js:297:30 at new Promise (<anonymous>) at open (internal/util.js:296:12) at WindowsBinding.open (C:\Users\user\wwsu-dj-controls\node_modules\@serialport\bindings\lib\win32.js:56:22) at processTicksAndRejections (internal/process/task_queues.js:97:5) ```
- What should have happened? The connection establishes successfully and we are able to begin writing (which was the case in electron 9 and bindings 9.0.2).
Code to Reproduce the Issue
// let device = device path retrieved from electron.settings, which I verified is operating correctly.
delaySerial = new serialport(device, {
baudRate: 38400,
});
delaySerial.on("error", (err) => {
serialError(err); // function sends the error both to the console and another logging utility. It also disconnects the serialport if not already disconnected and attempts to reconnect after a 15 second delay.
});
Versions, Operating System and Hardware
- SerialPort@? 9.0.4
- Node.js v? Electron 11.1.0
- Windows? Linux? Mac? Windows 10 build 2004
- Hardware and chipset? (Prolific/FTDI/Other) Unknown/other (uses USB connection, but this error occurs no matter which COM port I try)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 20
- Comments: 27 (5 by maintainers)
Encountered the same problem with Electron 11.1.1 and Serialport 9.0.4.
Adding
"rebuild": "electron-rebuild -f -w serialport"
to scripts and runningnpm run rebuild
solved it for me.I encountered same problem, too and I found the reason that cause this issue.
I used Electron 11.1, @serialport/bindings 9.0.4 (with serialport 9.0.4), electron-builder 22.9.1.
The reason what occurs this issue is wrong prebuilt image of @serialport/bindings@9.0.4.
By default,
electron-builder
using prebuilt image if exists. And there was a prebuilt image of @serialport/bindings.When I run the application after
electron-rebuild
was successfully working serialport@9.0.4 with @serialport/bindings@9.0.4.But after run the
electron-builder
, this issue reproduced with no code changes.And I looked into the logs of
electron-builder
, it uses prebuilt image of @serialport/bindings.So I add the option to force rebuild dependencies to the
electron-builder
option shots the problem.Packing time was slightly slowed-down but it was my best solution to using the latest version of serialport.
@Lovinity Encountered same problem. This problem was not found in serialport@9.0.3 + electron@8 though.
FWIW, upgrading from Electron 11 to Electron 12.0.2 solved this issue for me when all the other suggestions failed.
No. With 9.0.4, the “Third argument must be a function” error occurs either way (also after rebuilding). I just mentioned
electron-rebuild
to make it clear that, to use Electron 11, I had rebuilt the older (!) version ofserialport
with success. Sorry if that wasn’t clear 😉