Loading...
Loading...
Error: ENOENT: no such file or directory, open '/path/to/file'Node.js tried to access a file or directory that doesn't exist at the specified path. This is usually a path resolution issue — the path is relative to the wrong directory, or the file genuinely doesn't exist.
Use path.join with __dirname to get paths relative to the current file.
const path = require('path');
const fs = require('fs');
// ❌ Relative to process.cwd() — unreliable
const data = fs.readFileSync('./config.json');
// ✅ Relative to the current file — always works
const data = fs.readFileSync(path.join(__dirname, 'config.json'));Use fs.existsSync to guard against missing files.
const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, 'config.json');
if (fs.existsSync(filePath)) {
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
} else {
console.error('Config file not found:', filePath);
}