7

For some reason a function I am trying to use 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?

Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
  • Tested this excerpt on v17, works fine. Also checked the docs for `randomBytes` in v14.15, seems to exist, the module in itself seems to be around for a long time now. Try renaming the variable `crypto` to avoid name shadowing but appart from that, this is very cryptic. – Gaëtan Boyals Jan 03 '22 at 13:14
  • I like your pun. Yeah, it's quite strange :? – Jake Jackson Jan 03 '22 at 13:17
  • I have tested your snippet against the exact version of Node (`docker run -it node:14.15.1-slim node`) and was unable to reproduce the issue. – Paweł Kuffel Jan 03 '22 at 13:17
  • Try logging crypto, just to check it's the right module that is imported: `console.log(crypto);`, and compare it with the official docs, you should be able to see every function and constant mentioned in the docs. – Gaëtan Boyals Jan 03 '22 at 13:22

4 Answers4

2

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/

david
  • 243
  • 3
  • 17
1

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.

CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
1

If you attemped to create a token.

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

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

Ido Bar Lev
  • 426
  • 5
  • 6
0

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/