'Node.js crypto.randomBytes() is not a function
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?
Solution 1:[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.
Solution 2:[2]
If you attemped to create a token.
You can just type the following command in your nodejs cli:
crypto.randomBytes(64).toString('hex');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CodingYourLife |
| Solution 2 | Ido Bar Lev |
