easyHallucinated APIsNode.js

The helpful util

A developer asked an AI assistant to check whether a config file exists before reading it, using Node's promise-based fs module. The suggestion crashes on every single call, even when the file is present.

config.js
const fs = require('fs/promises');

async function readConfig(path) {
  if (!(await fs.exists(path))) {
    throw new Error(`Config not found at ${path}`);
  }
  return JSON.parse(await fs.readFile(path, 'utf-8'));
}

What's wrong with this function?

The function needs to validate that path is a string before use.
JSON.parse is called before readFile's promise resolves.
readFile should be called with a Buffer encoding instead of 'utf-8'.
fs/promises has no exists method: Node's promise-based fs API never shipped one.

Sign in with GitHub to submit an answer and track your progress.