How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Node.js · JavaScript

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

Error: ENOENT: no such file or directory, open '/app/config/settings.json' at Object.openSync (node:fs:603:3) at Object.readFileSync (node:fs:471:35) at loadConfig (src/config/loader.js:9:28) at Object.<anonymous> (src/index.js:4:18) at Module._compile (node:internal/modules/cjs/loader:1364:14) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._resolveFilename (node:internal/modules/cjs/loader:1019:15) at Function._load (node:internal/modules/cjs/loader:868:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Here's what each line means:

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.

Before (broken)
const fs = require('fs');

function loadConfig() {
  const raw = fs.readFileSync('./config/settings.json', 'utf8');
  return JSON.parse(raw);
}
After (fixed)
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:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. 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:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. 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)

  1. Verify the config file exists in the project and is committed to version control.
  2. Update the Dockerfile to COPY config/ into the image if applicable.
  3. Replace relative paths with __dirname-based absolute paths.
  4. Run tests locally to confirm the fix.
  5. 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.