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

TypeError: Third argument must be a function #2191

Closed
Lovinity opened this issue Dec 19, 2020 · 27 comments · Fixed by #2237
Closed

TypeError: Third argument must be a function #2191

Lovinity opened this issue Dec 19, 2020 · 27 comments · Fixed by #2237

Comments

@Lovinity
Copy link

Lovinity commented Dec 19, 2020

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)
@orange-beans
Copy link

@Lovinity Encountered same problem. This problem was not found in serialport@9.0.3 + electron@8 though.

@pklaschka
Copy link

Just for reference (this isn't really a solution, of course, if anything, it's an ugly hack): After encountering the same error, I was able to get it to work with Electron 11.1.0 and serialport in version 8.0.8 (after running electron-rebuild, of course) on both Windows and Linux.

Seeing

@Lovinity Encountered same problem. This problem was not found in serialport@9.0.3 + electron@8 though.

might, of course, mean, that a downgrade to an exact 9.0.3 might also have solved that problem, of course (I probably didn't properly specify the exact version, which has then lead me to believe 9.0.3 wasn't working either 😅 ).

@Lovinity
Copy link
Author

@pklaschka That's true it's a dirty solution. I unfortunately cannot rely on rebuild and must use prebuilt binaries due to limitations with concurrent jobs in the build tool I use (if I use matrices, not only does that drastically complicate things, but it might cost money too; so I have to build for all OSes on MacOS. And rebuild will only work for the MacOS build on MacOS).

@reconbot
Copy link
Member

@pklaschka so you're saying the prebuilds for windows in v9.0.4 have an issue but if you rebuild them they work for you?

@pklaschka
Copy link

@pklaschka so you're saying the prebuilds for windows in v9.0.4 have an issue but if you rebuild them they work for you?

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 of serialport with success. Sorry if that wasn't clear 😉

@reconbot
Copy link
Member

That's frustrating, nothing about the serialport code has changed between those versions.

@Lovinity
Copy link
Author

Maybe a dependency update is breaking the build?

@reconbot
Copy link
Member

Yeah, probably Nan which was required to build for the latest electron

https://github.com/serialport/node-serialport/blob/master/packages/bindings/src/serialport_win.cpp#L314-L317 is probably the code you're hitting. It's the write callback, which is guaranteed to have a callback here https://github.com/serialport/node-serialport/blob/master/packages/bindings/lib/win32.js#L10

I'll take a peak at some of the build warnings from the c++, maybe they've changed how functions work or something.

@naturalfreak
Copy link

Same problem

@gbmhunter
Copy link

gbmhunter commented Dec 23, 2020

Same problem here. It used to work fine, and then I started playing around with electron-rebuild and called it a number of times. Now I get this exact same error. Sorry this is vague, I can't really give any more details about it.

EDIT: O.K. I have more details:

COM port works fine in commit https://github.com/gbmhunter/ninjaterm-electron/commit/a02b89185891d9234b26c4473759231879581f2a
COM port does not work in commit https://github.com/gbmhunter/ninjaterm-electron/commit/69bd384161defe672378a1520314710c1abc3960

Wait, it now works in https://github.com/gbmhunter/ninjaterm-electron/commit/69bd384161defe672378a1520314710c1abc3960, after I re-ran yarn install. Ignore the two works/does not works commit, something more complex is going on.

@Kallontz
Copy link

Kallontz commented Dec 24, 2020

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.

"build": {
  ...
  "buildDependenciesFromSource": true,
  ...
}

Packing time was slightly slowed-down but it was my best solution to using the latest version of serialport.

@reconbot
Copy link
Member

Well... that's promising, it means a problem with the build vs the source. I removed the existing builds and forced another one can you try it again?

@orange-beans
Copy link

Well... that's promising, it means a problem with the build vs the source. I removed the existing builds and forced another one can you try it again?

Tried with the latest built binding (bindings-v9.0.4-electron-v85-win32-x64), the "TypeError: Third argument must be a function" error still occurs.

Tried to run electron-rebuild for serialport module, also failed #2194

@liyatang
Copy link

liyatang commented Dec 25, 2020

um, work for me change electron version to 10.1.3。 demo here

@Kallontz
Copy link

Well... that's promising, it means a problem with the build vs the source. I removed the existing builds and forced another one can you try it again?

I just got a same result as @orange-beans after flush the npm cache.

  • install prebuilt binary  name=@serialport/bindings version=9.0.4 platform=win32 arch=x64
  • execute command  command='{nodejs}\node.exe' '{Project}\node_modules\prebuild-install\bin.js' --platform=win32 --arch=x64 --target=11.1.1 --runtime=electron --verbose --force
                     workingDirectory={Project}\node_modules\@serialport\bindings
  • command executed  executable={nodejs}\node.exe
                      errorOut=prebuild-install info begin Prebuild-install version 6.0.0
    prebuild-install WARN install prebuilt binaries enforced with --force!
    prebuild-install WARN install prebuilt binaries may be out of date!
    prebuild-install info looking for cached prebuild @ {AppData\Roaming}\npm-cache\_prebuilds\a203aa-bindings-v9.0.4-electron-v85-win32-x64.tar.gz
    prebuild-install http request GET https://github.com/serialport/node-serialport/releases/download/@serialport/bindings@9.0.4/bindings-v9.0.4-electron-v85-win32-x64.tar.gz
    prebuild-install http 200 https://github.com/serialport/node-serialport/releases/download/@serialport/bindings@9.0.4/bindings-v9.0.4-electron-v85-win32-x64.tar.gz
    prebuild-install info downloading to @ {AppData\Roaming}\npm-cache\_prebuilds\a203aa-bindings-v9.0.4-electron-v85-win32-x64.tar.gz.35396-0b268dadf342e.tmp
    prebuild-install info renaming to @ {AppData\Roaming}\npm-cache\_prebuilds\a203aa-bindings-v9.0.4-electron-v85-win32-x64.tar.gz
    prebuild-install info unpacking @ {AppData\Roaming}\npm-cache\_prebuilds\a203aa-bindings-v9.0.4-electron-v85-win32-x64.tar.gz
    prebuild-install info unpack resolved to {Project}\node_modules\@serialport\bindings\build\Release\bindings.node
    prebuild-install info install Successfully installed prebuilt binary!

@Lovinity
Copy link
Author

Lovinity commented Jan 10, 2021

I think the problem (or one of the problems) is you need to bump the version up. Those of us who already downloaded 9.0.4 have a cache of it, and npm is not going to fetch the corrected build binary unless the version is bumped to 9.0.5.

Under semantic versioning, it is very bad practice to re-build something under the same version number.

"Once a versioned package has been released, the contents of that version MUST NOT be modified. Any modifications MUST be released as a new version."

@Erik7354
Copy link

Encountered the same problem with Electron 11.1.1 and Serialport 9.0.4.

Adding "rebuild": "electron-rebuild -f -w serialport" to scripts and running npm run rebuild solved it for me.

ijager added a commit to JitterCompany/SerialWatch that referenced this issue Feb 5, 2021
@TonyStark106
Copy link

Add

"build": {
  ...
  "npmRebuild": false,
  ...
}

And run electron-rebuild -f -w serialport work for me (two-package-structure should run electron-rebuild -m app -f -w serialport too). But cross compiling seem broken. Maybe a temporary solution for Electron 11.

@sammlapp
Copy link

I am encountering this issue as well. I have Electron 11.2.3 and Serialport 9.0.4.
Are any of these solutions "recommended" or consistently working? I have tried different serialport and electron versions without success. @TonyStark106 is the idea in your solution to add this "build" option to to package.json?

@TonyStark106
Copy link

TonyStark106 commented Feb 25, 2021

@sammlapp Yes! Like this:

{
  "scripts": {
    "postinstall": "electron-builder install-app-deps",
    "rebuild": "electron-rebuild -f -w serialport"
  },
  "dependencies": {
    "serialport": "9.0.4"
  },
  "devDependencies": {
    "electron": "11.2.3",
    "electron-builder": "22.9.1",
    "electron-rebuild": "2.3.5"
  },
  "build": {
    "npmRebuild": false
  }
}

Run yarn install && yarn rebuild will fix TypeError: Third argument must be a function when developing. Just Like @Erik7354 say. I encountered this issue again when distributing a release bundle unless add "npmRebuild" option simultaneously. But cross compiling seem broken after add "npmRebuild" option. You can only build in the same platform or wait for Serialport to support Electron 11.

@jgstephe
Copy link

jgstephe commented Apr 7, 2021

FWIW, upgrading from Electron 11 to Electron 12.0.2 solved this issue for me when all the other suggestions failed.

@Julusian
Copy link
Contributor

Julusian commented Apr 14, 2021

I have been digging and have an interesting discovery here.

I have created a very minimal project, which simply tries to open a serialport. And I can reproduce the issue when run with ELECTRON_RUN_AS_NODE=1 npx electron@^11.0.0 index.js

If inside node_modules/@serialport/bindings I issue:

  1. yarn add prebuild (its not listed as a dependency)
  2. yarn node-gyp rebuild --target=11.0.0-beta.12 --target_arch=x64 --runtime=electron --dist-url=https://ato m.io/download/electron to force a local build

Then back in the the root folder I try the ELECTRON_RUN_AS_NODE=1 npx electron@^11.0.0 index.js again, it works! It is also worth noting that a ELECTRON_RUN_AS_NODE=1 npx electron@11.0.0-beta.11 index.js does not work

But, if I try it with the rebuild being --target=11.0.0-beta.11, then it breaks again. But ELECTRON_RUN_AS_NODE=1 npx electron@11.0.0-beta.11 index.js now works.

At this point I was starting to be suspicious that beta.12 introduced a breaking change in the abi. For reference the node-abi project uses 11.0.0-beta.11 as the version which stabilised the abi, and so that is the version that prebuild used when building the binaries for this project.

Some skimming of the git diff between those electron versions later, and I found this change electron v11.0.0-beta.11...v11.0.0-beta.12
So I manually applied that patch to my node-gyp cache of the headers (in C:\Users\Julus\AppData\Local\node-gyp\Cache\11.0.0-beta.11\include\node\common.gypi) and then repeat the rebuild from above.
Try running it and it works again!

So it looks like node-abi is giving out a version number that is broken. electron-rebuild will be working because it will build against the exact version of electron you are using.
Also cmake-js/cmake-js#222 (comment) appears to have discovered the same missing define.

As for solutions, the quickest option would be to add 'defines': ['V8_REVERSE_JSARGS'], to binding.gyp. Probably behind a check to only apply to electron 11, to minimise risk of other breakages. I will look into a PR for this tomorrow.
It would be a good idea to chase this upstream in node-abi, prebuild or electron, but it would be good to have some other native libraries that are broken in the same way to help prove where the issue lies. Does anyone know of any other nan based libraries using prebuild with prebuilds made for electron 11?

@samjonesigd
Copy link

Hi all

When i do a rebuild it's fine on electron:serve but when I go to build it as a windows or mac app I get the error in the app.

Is there anything I can do to get around this for now?

@mussacharles60
Copy link

mussacharles60 commented May 4, 2021

This one electron-rebuild -f -w serialport works for me on electron@v11.2.3 and serialport@9.0.7

@JeongJun-Lee
Copy link

same here. how can i always force rebuild this in prod build script whenever production build?

@samjonesigd
Copy link

For anyone reading I eventually got this working by using electron 10 and serialport 9.0.3 & rebuilding with electron-rebuild

@chenzeshu
Copy link

chenzeshu commented Jun 29, 2021

win10

 "dependencies": {
     "serialport": "^9.0.7",
  },
  "devDependencies": {
    "electron": "11.0.0",
  },

"electron-rebuild -f -w serialport" works for me!
thx!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.