ENOENT:No Such File Or directory

1 minute read | 3 months ago

When I was deploying a NextJS project to Vercel that involved reading local files. Upon reading a file, I encountered the following error message(s).

[Error: ENOENT: no such file or directory, open '__components__/components/ActivityIndicator/index.tsx'] { errno: -2, code: 'ENOENT', syscall: 'open', path: '__components__/components/ActivityIndicator/index.tsx', digest: '739059591' }

After almost 3 days of debugging and research, I came to realize that "There are some cases in which Next.js might fail to include required files, or might incorrectly include unused files. In those cases, you can leverage outputFileTracingExcludes and outputFileTracingIncludes respectively in next.config.js. Each config accepts an object with minimatch globs for the key to match specific pages and a value of an array with globs relative to the project's root to either include or exclude in the trace." Upon realizing this, I updated my next.config.js file to include my files/folders, and everything worked fine.

const nextConfig = { // ...Other config outputFileTracingIncludes: { // This will include the __components__ folder for all pages "/**/*": ["./__components__/**/*"], }, };

It is also worth mentioning that, in dev, this problem does not happen, it only happens in prod. Reference: Output