RIYAJ KHAN
RIYAJ KHAN

Reputation: 15290

TypeError: Buffer.alloc is not a function

From node docs, I am trying some code from Buffer .

When I execute this statement

const buf = Buffer.from('hello world', 'ascii');

it throws an exception: TypeError: Buffer.alloc is not a function

My node version is v5.7.1.

I don't understand why?

Here is the error screen.

enter image description here

Upvotes: 14

Views: 23106

Answers (6)

Sneha Mukkala
Sneha Mukkala

Reputation: 1

I have also faced the same issue.
Just do Buffer.Buffer.alloc(), it works fine then :)

hope it helps

Upvotes: 0

azuldev
azuldev

Reputation: 694

For whatever reason my alloc function was a couple objects down. Please verify your console.log(Buffer)

   let Buffer = require('buffer');
   console.log(Buffer)
   data = Buffer.Buffer.alloc(0);

Upvotes: 0

NickJ
NickJ

Reputation: 536

If you don't want to restart you can just do this:

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
sudo apt install nodejs
node -v

Instructions from here: https://websiteforstudents.com/install-the-latest-node-js-and-nmp-packages-on-ubuntu-16-04-18-04-lts/

Upvotes: 0

if you can do these commands,it should be work.

step 1:

first, you can type this command sudo npm cache clean -f

step 2: Now,you can type this command sudo npm install -g n

step 3:

Now ,you should enter this command sudo n 6.4.0

step 4:

finally, you should enter this command for restart the your system or reboot

shutdown -h now

Upvotes: 1

Pankaj Chauhan
Pankaj Chauhan

Reputation: 1715

This error occurs due to missmatch node version so please node version on my machine like this:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Upvotes: 4

mscdex
mscdex

Reputation: 106736

The new Buffer API is only available in v5.10.0+, so you'll need to upgrade to use it.

If you want to view the docs for v5.7.1, you can go here.

FWIW the new Buffer API is also now available in node v4.x as of v4.5.0.

Upvotes: 16

Related Questions