I’m encountering the following error after deploying my Next.js app on Vercel:
[Error: ENOENT: no such file or directory, open '/var/task/problems/add-two-numbers/boilerplate-full/function.java'] { errno: -2, code: 'ENOENT', syscall: 'open', path: '/var/task/problems/add-two-numbers/boilerplate-full/function.java', digest: '3522486023' }
This error occurs when the backend tries to access a .java or any other file in the /var/task/problems directory. The file exists locally and works fine in my development environment, but it doesn’t seem to be found on Vercel.
My Project Structure:
next-app:
--problems:
----add-two-numbers:
------boilerplate-full:
--------function.java
--------function.py
--------function.js
------inputs:
--------0.txt
--------1.txt
------outputs:
--------0.txt
--------1.txt
----other-problems:
--src:
----actions:
------problem.js:
----app:
------problem/[...problemSlug]/page.js
------page.jsx
------layout.jsx
src/actions/problem.js:
"use server";
import fs from "fs";
import path from "path";
import { LanguagesToExtension } from "./types";
const MOUNT_PATH = path.join(process.cwd(), "problems");
export const getProblem = async (problemSlug, languageId) => {
languageId = LanguagesToExtension[languageId];
const fullBoilderPlate = await getProblemFullBoilerplateCode(
problemSlug,
languageId
);
const inputs = await getProblemInputs(problemSlug);
const outputs = await getProblemOutputs(problemSlug);
return {
id: problemSlug,
fullBoilerplateCode: fullBoilderPlate,
inputs: inputs,
outputs: outputs,
};
};
async function getProblemFullBoilerplateCode(problemSlug, languageId) {
return new Promise((resolve, reject) => {
fs.readFile(
`${MOUNT_PATH}/${problemSlug}/boilerplate-full/function.${languageId}`,
{ encoding: "utf-8" },
(err, data) => {
if (err) {
reject(err);
}
resolve(data);
}
);
});
}
async function getProblemInputs(problemSlug) {
return new Promise((resolve, reject) => {
fs.readdir(
`${MOUNT_PATH}/${problemSlug}/tests/inputs`,
async (err, files) => {
if (err) {
console.log(err);
} else {
await Promise.all(
files.map((file) => {
return new Promise((resolve, reject) => {
fs.readFile(
`${MOUNT_PATH}/${problemSlug}/tests/inputs/${file}`,
{ encoding: "utf-8" },
(err, data) => {
if (err) {
reject(err);
}
resolve(data);
}
);
});
})
)
.then((data) => {
resolve(data);
})
.catch((e) => reject(e));
}
}
);
});
}
async function getProblemOutputs(problemSlug) {
return new Promise((resolve, reject) => {
fs.readdir(
`${MOUNT_PATH}/${problemSlug}/tests/outputs`,
async (err, files) => {
if (err) {
console.log(err);
} else {
await Promise.all(
files.map((file) => {
return new Promise((resolve, reject) => {
fs.readFile(
`${MOUNT_PATH}/${problemSlug}/tests/outputs/${file}`,
{ encoding: "utf-8" },
(err, data) => {
if (err) {
reject(err);
}
resolve(data);
}
);
});
})
)
.then((data) => {
resolve(data);
})
.catch((e) => reject(e));
}
}
);
});
}
Earlier, the position of problems folder is in src folder.
I have also used
const MOUNT_PATH = path.relative(_dirname,”src”,”problems”); &&
const MOUNT_PATH = path.join(“src”, “problems”);
Ensured the file paths are correct.
Checked that the file is committed to GitHub and included in the build.
I’m unsure if this issue is related to Vercel’s file system or a configuration error.
GitHub Repository:
https://github.com/chman-chaudhary/gla-Codify
Any help to resolve this would be appreciated! Thanks in advance!