easyHallucinated APIsNode.js
The phantom module
A junior developer asked a chatbot to write a script that generates a cryptographically secure random identifier. The AI generated the following code, claiming it uses Node.js standard libraries. What is the issue here?
generate-id.js
const crypto = require('crypto');
function generateUserSessionId() {
// Generate a random ID using standard crypto module
const rawToken = crypto.generateRandomToken(32);
return rawToken.toString('hex');
}
console.log(generateUserSessionId());What is the primary bug in the AI-generated code?
crypto.generateRandomToken is not a standard Node.js API method; it should be crypto.randomBytes.
The crypto module must be installed via npm first; it is not a built-in Node.js module.
rawToken.toString() does not accept 'hex' as an encoding format; it only supports 'utf8'.
The function generates synchronous blockages and should be wrapped in an async/await block.