Error: ENOENT: no such file or directory, open '/app/config.json'
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
Your code is trying to read, write, or access a file or directory that doesnβt exist at the specified path.
Fix 1: Wrong File Path
// β Relative path depends on where you run the script
fs.readFileSync('config.json'); // Looks in CWD, not script dir
// β
Use __dirname or import.meta
const path = require('path');
fs.readFileSync(path.join(__dirname, 'config.json'));
// ESM
const configPath = new URL('./config.json', import.meta.url);
Fix 2: File Doesnβt Exist Yet
// β Reading a file that hasn't been created
const data = fs.readFileSync('output.json');
// β
Check if it exists first
if (fs.existsSync('output.json')) {
const data = fs.readFileSync('output.json');
} else {
fs.writeFileSync('output.json', '{}');
}
Fix 3: Missing Directory
// β Writing to a directory that doesn't exist
fs.writeFileSync('logs/app.log', 'hello'); // π₯ If logs/ doesn't exist
// β
Create the directory first
fs.mkdirSync('logs', { recursive: true });
fs.writeFileSync('logs/app.log', 'hello');
Fix 4: npm/Node Modules Missing
# ENOENT during npm install or npm start
# β node_modules is corrupted or missing
# β
Clean install
rm -rf node_modules package-lock.json
npm install
Fix 5: Docker β File Not Copied
# β File exists locally but not in the container
COPY . .
# Did you forget to include it? Check .dockerignore
# β
Verify the file is in the build context
RUN ls -la /app/config.json
Fix 6: Environment-Specific Paths
// β Hardcoded path that only works on your machine
const config = '/Users/john/project/config.json';
// β
Use environment variables or relative paths
const config = process.env.CONFIG_PATH || './config.json';
Debugging
# Check if the file exists
ls -la /path/to/file
# Check your current working directory
pwd
# Node.js β log the resolved path
console.log(path.resolve('config.json'));