Jake Jackson
Jake Jackson

Reputation: 1235

Node.js crypto.randomBytes() is not a function

For some reason a function I am calling is apparently not a function.

Welcome to Node.js v14.15.1.
Type ".help" for more information.
> const crypto = require("crypto");
undefined
> x = crypto.randomBytes(32).toString("hex")
Uncaught TypeError: crypto.randomBytes is not a function

Documentation for randomBytes().

Is there something I am not understanding?

Upvotes: 10

Views: 40911

Answers (7)

Munim
Munim

Reputation: 2768

Late to the party, but according to the official doc this is the right way to use it btw-

const {
  randomBytes,
} = require('node:crypto');

const buf = randomBytes(256);
console.log(buf.toString('hex'));

Upvotes: 0

Mike Petterson
Mike Petterson

Reputation: 1

All you have to do is assign the require statement to a variable named something other than "crypto".

For example (prepending an underscore _):

const _crypto = require("crypto");
var x = _crypto.randomBytes(32).toString('hex');

When importing with the name crypto, the function randomBytes takes two arguments, the second being a callback function. So if you want to use it as intended, use as follows:

const crypto = require("crypto");

crypto.randomBytes(32, function(tokenErr, buffer){
    if(tokenErr){
        console.log(tokenErr);
    }else{
        console.log(buffer.toString('hex');
    }
})

Upvotes: 0

Daniel Ray
Daniel Ray

Reputation: 133

For all who is NodeJS and seeking for answer:

import * as crypto from 'crypto';

Upvotes: 12

Brajbhooshan S
Brajbhooshan S

Reputation: 1

I am facing the same issue, after that, I upgrade the node js version 14 to 16, and Its works for me. Please try to upgrade the node js version.

Ubuntu Install: https://computingforgeeks.com/how-to-install-node-js-on-ubuntu-debian/

Windows Install: https://nodejs.org/en/blog/release/v16.16.0/

Mac Os Install: https://nodesource.com/blog/installing-nodejs-tutorial-mac-os-x/

Upvotes: 0

david
david

Reputation: 263

I ran into this same error in the command line: Uncaught TypeError: crypto.randomBytes is not a function

This did NOT work for me:

$ node
> require("crypto")
> crypto.randomBytes(32).toString("hex")

Crypto and randomBytes have to be called in the same command:

$ node
> require('crypto').randomBytes(32).toString('hex')

The output is something like this:

'7a3161b8c92dbf26f0717e89edd27bf10094d2f5cc0f4f2d70d08f463f2881db'

After a good bit of searching, I finally found the solution here: https://massimilianomarini.com/2020/04/random-string/

Upvotes: 4

Ido Bar Lev
Ido Bar Lev

Reputation: 506

If you attemped to create a token.

You can just type the following command in your nodejs cli:

crypto.randomBytes(64).toString('hex');

Upvotes: 2

CodingYourLife
CodingYourLife

Reputation: 8596

Seems getRandomBytes() function got removed. I read some disclaimers that it is not very secure.

https://www.npmjs.com/package/crypto is clustured with deprecation messages so altough most upvotes here under https://stackoverflow.com/a/8856177/828184 it does no longer seem state of the art to me.

Before I could simply use (like you but no longer after package updates)

import crypto from "crypto";
const token = crypto.randomBytes(64).toString('hex');

But crypto now only has getRandomValues() and I think it is not a replacement.

Only answer nr 3 with also a lot but not as many upvotes gave me a working version https://stackoverflow.com/a/25690754/828184. So maybe also try:

import { nanoid } from "nanoid";
const token = nanoid(64); //instead of crypto.randomBytes(64).toString('hex')

And leave an upvote there if it works because.

Upvotes: 1

Related Questions