# Help with nodejs changes to new Buffer()

**URL:** <https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753>\
**Category:** General\
**Created:** [17 November 2022 06:14 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753 "2022-11-17T06:14:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![emlarsen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emlarsen/32/889_2.png) [@emlarsen](https://discourse.nodered.org/u/emlarsen)\
**Post date:** [17 November 2022 06:14 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/1 "2022-11-17T06:14:49Z")

</div>

I have a function node that extracts some numbers from binary data. It has been running for years wonderfully but of course I decided to upgrade nodejs and nodered and new Buffer is no longer working and I can seem to understand how to change it to the new safe-buffer.

Can anyone tell me what I’m doing wrong converting new Buffer() to the new safe buffer..

Original code that worked until I upgraded…

var amps = [msg.payload.data[0],msg.payload.data[1]];  
var ampsBuff = new Buffer(amps);  
amps = ampsBuff.readInt16LE(0)  
global.set('globalAmps', amps);

I have tried changing  
var ampsBuff = new Buffer(amps) to var ampsBuff = Buffer.from(amps);

I have also tried changing  
var ampsBuff = new Buffer(amps) to var ampsBuff = Buffer.alloc(amps);

Sorry buffers are not my strength. I’m just not sure how to create this safe-buffer so that I can extract the number from it.

Thanks for any help

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [17 November 2022 06:31 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/2 "2022-11-17T06:31:33Z")

</div>

Hi!

> [@emlarsen](#):
>
> var ampsBuff = Buffer.alloc(amps);

... can't work, as `Buffer.alloc(size)` only crates an empty buffer of size `size`.

> [@emlarsen](#):
>
> var ampsBuff = Buffer.from(amps);

... looks ok though - according to [the documentation](https://nodejs.org/api/buffer.html#static-method-bufferfromarray).

To support further, it would be helpful to know the exact issue you have...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 November 2022 07:00 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/3 "2022-11-17T07:00:59Z")

</div>

Also tell us what version you have upgraded to. It should be 16 or 18.

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [17 November 2022 09:25 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/4 "2022-11-17T09:25:30Z")

</div>

Maybe some of this can get you going.

```auto
[{"id":"40bab1327d0a160d","type":"inject","z":"f1cd617e3bd1934a","name":"","props":[{"p":"payload.data","v":"[44,55]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":460,"wires":[["203fec3416a1e8eb"]]},{"id":"203fec3416a1e8eb","type":"function","z":"f1cd617e3bd1934a","name":"function 35","func":"let msg1 = {}\nlet amps = [msg.payload.data[0], msg.payload.data[1]]\nlet ampsBuff = Buffer.from(msg.payload.data)\nlet ampsBuff1 = ampsBuff.readInt16LE(0)\n\nmsg1.payload = ampsBuff1\nreturn msg1;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":460,"wires":[["cb5f68233100f104"]]},{"id":"cb5f68233100f104","type":"debug","z":"f1cd617e3bd1934a","name":"debug 98","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":640,"y":460,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![emlarsen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emlarsen/32/889_2.png) [@emlarsen](https://discourse.nodered.org/u/emlarsen)\
**Post date:** [17 November 2022 17:07 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/5 "2022-11-17T17:07:10Z")

</div>

Got it figured out.. I looked at the raw data and there was nothing there.. turns out two things went wrong at the same time. Hardware wiring had come undone. So wired that up and then changed all the new Buffer() to Buffer.From() and it all works fine…

Thanks for taking the time to offer help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 December 2022 17:08 UTC](https://discourse.nodered.org/t/help-with-nodejs-changes-to-new-buffer/70753/6 "2022-12-01T17:08:02Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
