I'm trying to send a udp packet using the node.js dgram package from an electron app. The send method calls for a Buffer, Uint8Array, or string. I'm getting a type error when I try to use a Buffer or UintArray however. The exact error is: TypeError: First argument must be a buffer or a string.
I can see in the Node.js dgram code that it is checking for a buffer using !(buffer instanceof Buffer
. If I try using instanceof on the buffer in my code it just returns object as the type, while if I try 'Buffer.isBuffer()' it returns true. I've tried all the methods that node provides for creating buffers buffer with no results. Send does accept strings but then I am running into all sorts of issues with encoding, and that feels kinda of hacky.
It looks like electron is running node 7.4.0.
I see my options from here as:
- Edit the node.js code
- Somehow override what instanceof returns for my buffer object
- Figure out a solution for properly encoding the Buffer as a string.
Any ideas?
I'm trying to send a udp packet using the node.js dgram package from an electron app. The send method calls for a Buffer, Uint8Array, or string. I'm getting a type error when I try to use a Buffer or UintArray however. The exact error is: TypeError: First argument must be a buffer or a string.
I can see in the Node.js dgram code that it is checking for a buffer using !(buffer instanceof Buffer
. If I try using instanceof on the buffer in my code it just returns object as the type, while if I try 'Buffer.isBuffer()' it returns true. I've tried all the methods that node provides for creating buffers buffer with no results. Send does accept strings but then I am running into all sorts of issues with encoding, and that feels kinda of hacky.
It looks like electron is running node 7.4.0.
I see my options from here as:
- Edit the node.js code
- Somehow override what instanceof returns for my buffer object
- Figure out a solution for properly encoding the Buffer as a string.
Any ideas?
Share Improve this question asked Jun 19, 2017 at 23:26 Russill GloverRussill Glover 711 silver badge6 bronze badges 2-
Did you use
Buffer.isBuffer()
, like on the classBuffer
or on your actual instance? Have you also tried usingBuffer.from()
to create your buffer? – RoyalBingBong Commented Jun 20, 2017 at 9:44 - I've tried using Buffer.from and new Buffer(). Also I was mistaken, if I try instanceof Buffer on my buffer it is returning true. I think the issue may be with the Node dgram module relying on a different version of Buffer. Is there anyway I can change the node code with repiling? – Russill Glover Commented Jun 20, 2017 at 16:47
1 Answer
Reset to default 5To get this to work I just had to point my global Buffer value to the node buffer module as so: const Buffer = window.require('buffer').Buffer