Npm Run Build Error: EMFILE: too many open files, open export-detail.json errno: -4066

The “npm run build” step has begun throwing the following error when building on circle.ci:

Build error occurred
[Error: EMFILE: too many open files, open ‘C:\Users\circleci\project.next\export-detail.json’] {
errno: -4066,
code: ‘EMFILE’,
syscall: ‘open’,
path: ‘C:\Users\circleci\project\.next\export-detail.json’
}

I do not get the error on my local development box, or on our development or production servers. Only on Circle.ci. I have googled everything I can find on the topic and applied any relevant potential fixes, but as of yet the error is still occurring. Therefore I am reaching out to the Circle.ci community to ideas and suggestions.

Here is the relevant part of my config.yml

version: 2.1

commands:
  installdependencies:
    description: 'Installs dependencies and caches for reuse'
    steps:
      - restore_cache:
          keys:
            - nvm-cache-v2-18.20.4
      - run:
          command: |
            nvm install 18.20.4
            nvm use 18.20.4
      - save_cache:
          key: nvm-cache-v2-18.20.4
          paths:
            - C:\programData\nvm\v18.20.4
      - restore_cache:
          keys:
            - node-modules-cache-v1-{{ checksum "package-lock.json" }}
      - run:
          command: npm I
      - save_cache:
          key: node-modules-cache-v1-{{ checksum "package-lock.json" }}
          paths:
            - ./node_modules

orbs:
  win: circleci/windows@2.4.0

jobs:
  build:
    executor:
      name: win/default
      shell: bash.exe
    steps:
      - checkout
      - installdependencies
      - run:
          command: npm run build

workflows:
  lint-test-build-app:
    jobs:
      - build

Any insights or guidance would be greatly appreciated!

Thank you!

I found a solution, or maybe a more accurate statement is I found a workaround. I decided to create a brand new branch in GIT and manually move all changes from the troubled branch over to the new branch. I verified the changes locally and then pushed to GIT, created a new pull request, and waited for circle.ci to verify the new branch. And this time the build succeeded without incident.
Maybe the branch was corrupt somehow, although it worked fine everywhere but within circle.ci. Maybe it was because the branch had too many checkins. 8 checkins when the error began to appear. The root cause is still a mystery, but hopefully my workaround helps others.

Update: My previously listed workaround turned out to be very temporary, as the error returned on the next commit and build attempt. Circle.ci support finally got involved and suggested trying the following:

  • Check for unnecessary imported libraries, which I already tried and addressed.
  • If we had upgraded to a newer version of Node, try downgrading to a version of Node that was working. We never upgraded Node.
  • Try “rm -rf node_modules”. Did not help.
  • Try “rm package-lock.json”. This helped and appears to be a longer term solution, as the error has not returned even after subsequent commits and builds.

After deleting the old package.json file, and generating a new one, it reduced the file size by 40%, and removed old packages that were no longer required and potentially causing the issue. The only down side to this solution is that creating a new package.json file changes what files are loaded and what order they are loaded, which caused a few issues and took some time to work through.

I am certainly hoping we can put this issue behind us now.