process.cwd() Different paths in development and production environment #11197
-
QuestionI'm using next js 14. In each page folder I have a file (meta.md) that I read using fs. Let it be written accordingly to these files In the development environment, everything works and the files are read, this is understandable, since process.cwd refers to the root directory of the project. But on the vercel, process.cwd refers to .next, where naturally the meta.md file no longer exists. I have some problems with debugging on Vercel and I can't keep track of everything. But there is some confusion about how this works in production, since I call my function that reads the files in the top page.tsx (homepage) file and it surprisingly works. Doesn't work if the md file reading function is called at levels below (another pages in another folders) |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
I see that this is a common problem related to verceland the file system in production. I tried to implement this in different ways, but it doesn't seem to work |
Beta Was this translation helpful? Give feedback.
-
Don't know if any of you was able to fix the issue. I managed to go around the issue by putting the .md files I needed to open in the lib folder. Turns out vercel always includes the lib folder as part of the assets under src/lib. That way I was able to open the files with process.cwd() both locally and on vercel. |
Beta Was this translation helpful? Give feedback.
-
I was struggling with a similar issue. Not sure if the solution found will work for you. If you render something like " {process.cwd()} " in prod (app deployed in Vercel), it renders the directory path "var/task".Which as expected, is different from what one will get in a local environment (npm run dev). So, I tried to render something like this: const filesDirectoryList = fs.readdir(process.cwd()) {filesDirectoryList.map(file => ( {file} )}And got a list of files where "public" directory was not visible. import fs from "fs"; const filePath = path.join(process.cwd(), "public", "example.md"); You may add a more complex hierarchy of folders like "posts/example.md" and it also works. It did the job, I was able target the desired files. |
Beta Was this translation helpful? Give feedback.
-
There's some more details about this files get included here: https://vercel.com/guides/how-can-i-use-files-in-serverless-functions Do do you have a small example of the issue you're seeing? |
Beta Was this translation helpful? Give feedback.
-
I solved this issue. /*The posts folder is in the root of the project*/
const pathToPosts = process.env.NODE_ENV === "development" ? "/posts" : "posts";
const postsDirectoryPath = process.cwd();
const files = await fs.readdir(path.join(postsDirectoryPath, pathToPosts), {
withFileTypes: true,
}); |
Beta Was this translation helpful? Give feedback.
-
Might also be related to this. If you're using See the official documentation /** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone",
experimental: {
turbotrace: {},
},
}; EDIT: |
Beta Was this translation helpful? Give feedback.
-
Running Next 14. export async function getBlogPosts(
blogDirectory: string = BLOG_CONTENT_PATH
): Promise<Optional<PostData[]>> {
console.log(
'Invoked all blog posts from: ' + path.join(process.cwd(), blogDirectory)
);
return getMDXData(path.join(process.cwd(), blogDirectory));
} I get this on vercel
Local production build
This solved it /** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
outputFileTracingIncludes: {
'/blog': ['./public/**/*'],
},
},
} |
Beta Was this translation helpful? Give feedback.
-
Works for me, thanks 😊 |
Beta Was this translation helpful? Give feedback.
I solved this issue.
Everything is quite logical.
However, I don’t quite remember the decision, since I haven’t touched the project for more than two months.
But here is an example with posts that I implemented