Fix Error: ENOENT: no such file or directory, open '/app/config/settings.json' in Node.js
This error means Node.js tried to open a file that does not exist at the specified path. Common causes include wrong relative paths, missing files in Docker builds, or incorrect working directory assumptions. Fix it by using path.resolve with __dirname and verifying the file exists before reading.
Reading the Stack Trace
Here's what each line means:
- at Object.openSync (node:fs:603:3): Node's fs module tried to open the file synchronously but the OS reported the path does not exist.
- at loadConfig (src/config/loader.js:9:28): Your config loader at line 9 calls readFileSync with a path that does not resolve to an existing file.
- at Object.<anonymous> (src/index.js:4:18): The config is loaded at application startup on line 4 of index.js, so the app crashes immediately.
Common Causes
1. Relative path resolves differently in different environments
Using a relative path like './config/settings.json' works from the project root but fails when the app is started from a different directory.
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('./config/settings.json', 'utf8'));
2. File not copied into Docker image
The Dockerfile does not COPY the config directory, so the file is missing at runtime inside the container.
# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY src/ ./src/
# Missing: COPY config/ ./config/
3. File deleted or renamed without updating references
The config file was renamed or moved but the code still references the old path.
// File was renamed to app-config.json but code still references settings.json
const config = JSON.parse(fs.readFileSync('./config/settings.json', 'utf8'));
The Fix
Use path.resolve with __dirname to build an absolute path relative to the source file, not the working directory. Check that the file exists before reading it to produce a clear error message instead of a raw ENOENT.
const fs = require('fs');
function loadConfig() {
const raw = fs.readFileSync('./config/settings.json', 'utf8');
return JSON.parse(raw);
}
const fs = require('fs');
const path = require('path');
function loadConfig() {
const configPath = path.resolve(__dirname, '../config/settings.json');
if (!fs.existsSync(configPath)) {
throw new Error(
`Config file not found at ${configPath}. ` +
`Ensure config/settings.json exists and is included in your build.`
);
}
const raw = fs.readFileSync(configPath, 'utf8');
return JSON.parse(raw);
}
Testing the Fix
const fs = require('fs');
const path = require('path');
jest.mock('fs');
const { loadConfig } = require('./loader');
describe('loadConfig', () => {
it('reads config from the correct absolute path', () => {
fs.existsSync.mockReturnValue(true);
fs.readFileSync.mockReturnValue('{"port": 3000}');
const config = loadConfig();
expect(config.port).toBe(3000);
expect(fs.readFileSync).toHaveBeenCalledWith(
expect.stringContaining('config/settings.json'),
'utf8'
);
});
it('throws a descriptive error when file is missing', () => {
fs.existsSync.mockReturnValue(false);
expect(() => loadConfig()).toThrow('Config file not found');
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/nodejs-file-not-found,git add src/config/loader.js src/config/__tests__/loader.test.js,git commit -m "fix: use absolute path for config loading and check existence",git push origin fix/nodejs-file-not-found
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
const { initBugStack } = require('bugstack-sdk')
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Verify the config file exists in the project and is committed to version control.
- Update the Dockerfile to COPY config/ into the image if applicable.
- Replace relative paths with __dirname-based absolute paths.
- Run tests locally to confirm the fix.
- Open a PR and merge after CI passes.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other modules are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Docker images only contain files explicitly COPYed in the Dockerfile. If the config directory is not included in a COPY instruction, it won't exist at runtime inside the container.
For secrets and environment-specific values, yes. For complex structured configuration, a config file is fine as long as you handle the path correctly and include it in your deployment artifacts.