Issue encountered: ENOENT error occurred due to the absence of the file or directory specified as '/var/task/src/content/hello.mdx'. Problem identified during the Next.js blog deployment on Vercel for production

I'm currently in the process of setting up a blog section on my personal website. However, I've encountered an error that's preventing me from accessing a specific blog post:

(Vercel logs)

Error: ENOENT: no such file or directory, open '/var/task/src/content/hello.mdx'
    at Object.openSync (node:fs:601:3)
    at Object.readFileSync (node:fs:469:35)
    at PostPage (/var/task/.next/server/app/posts/[slug]/page.js:381:45)
    at S (/var/task/.next/server/chunks/789.js:6503:13)
    at eb (/var/task/.next/server/chunks/789.js:6618:21)
    at Array.toJSON (/var/task/.next/server/chunks/789.js:6422:20)
    at stringify (<anonymous>)
    at pb (/var/task/.next/server/chunks/789.js:6829:9)
    at mb (/var/task/.next/server/chunks/789.js:6728:29)
    at Timeout._onTimeout (/var/task/.next/server/chunks/789.js:6553:16) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/var/task/src/content/hello.mdx'
}

Upon attempting to view a blog post on my portfolio site, the following error message pops up:

Application error: a server-side exception has occurred (see the server logs for more information). Digest: 2113801254

This is the current structure of my project directory:

my-app/
└── src/
    ├── app/
    │   ├── posts/
    │   │   ├── page.js
    │   │   └── [slug]/
    │   │       └── page.js or PostPage
    │   └── page.js
    ├── layout.js
    ├── page.js
    ├── components
    ├── content/
    │   └── hello.mdx
    └── fonts

The error indicates that there's an issue with the PostPage file. Here's the code snippet causing the problem:

export default function PostPage({ params }) {
  const post = fs.readFileSync(
    path.join(__dirname, "../../../../../src/content/" + params.slug + ".mdx"),
    "utf-8"
  );
  const { data: frontmatter, content } = matter(post);
  const blog = DOMPurify.sanitize(
    marked.parse(content, { mangle: false, headerIds: false })
  );
.....
//returns jsx with markdown content
}

I've attempted adjusting the path in the readFileSync() function without success in production. It does work in local dev mode. Any assistance would be greatly appreciated.

Just a beginner in programming and navigating tools like Next.js and Vercel. Thank you!

Answer №1

The path you are currently using is incorrect ->

path.join(__dirname, "../../../../../src/content/" + params.slug + ".mdx"),
. Remember that '__dirname' represents your root directory. Consider logging __dirname or utilizing process.cwd() instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most effective way to remove harmful HTML (such as XXS) from content submissions?

My content submission form has various input fields that are directly entered into the database upon submission and then printed when requested. After recognizing this as a security concern, I am looking for a way to remove malicious HTML (XSS) while stil ...

Having trouble dynamically adding elements to the Semantic-UI Accordion component

I have a challenge involving inserting dynamic content into a Semantic-UI Accordion. My goal is to display JSON data as an Accordion in the HTML. Below is the script and HTML I am using for this purpose: <script language='javascript'> ...

Change the color of specific elements in an SVG using React

Can I change the dynamic primary color from ReactJS to a specific class in an SVG file? If yes, how can it be done? Error.svg <!-- Generator: Adobe Illustrator 26.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1&qu ...

Invoker of middleware and stack functions for Express.js with a focus on capturing the response object

It appears that the expressjs app contains a stack of Layer object Arrays. What function is utilized to pass the I am curious about: When a request is sent from the http client, which function is called first and how are the stack array functions with mi ...

Is it possible to initiate validation on an HTML element without the presence of a form?

Is it possible to trigger validation on an HTML element without using a form? For example, I have set the required attribute on a field, but when I click the Save button, the field doesn't display the red outline indicating validation failure. I susp ...

Employing a one-time use variable that is asynchronously loaded via a React hook

Currently, I am exploring the functionalities of React's hooks, but I'm encountering a roadblock when it comes to integrating different use cases. The main goal is to create a hook called useNationsAsync that fetches a list of available nations ...

Exploring the fundamentals of authentication with Express JS

Currently, I am encountering a challenge while attempting to implement basic authentication with Username and Password using Express JS. The problem lies in the fact that when I try to incorporate an if statement within an app.use() function, it does not s ...

Is there a way to refresh the current page in Ionic 3 when the browser is reloaded?

I have encountered a problem in my work with an Ionic 3 App. I am looking for a solution where the browser will refresh the current page instead of redirecting me to the home page. Is this achievable? I attempted to solve it using the following code: thi ...

I am experiencing difficulty in successfully transmitting a variable from my jQuery code to my PHP code

I've been attempting to pass a variable from my jQuery code to my HTML/PHP code using AJAX and POST. However, I'm encountering an error message stating "Notice: Undefined index: testData in C:\xampp\htdocs\teszt\test1.php on l ...

Having trouble with fetch() not working in Next.JS while securing API Routes with auth.js

Currently, I am working on a project that involves user authentication using auth.js in next.js. The main goal is to create an API that retrieves specific user information from a MongoDB Database. The website itself is secured with middleware in next.js. I ...

Multiple calls being made to $on function within $rootScope

After my webservice completes data retrieval, I trigger $rootScope.$emit('driver-loader');. This signal is only sent from this particular location. In order to listen for 'driver-loaded', I have the following code snippet: var watchDri ...

What is the best way to incorporate an if else condition using the <?php if($loggedin): ?> statement within JavaScript code to display a button push or pop response from the server side?

I would like to verify this php if condition code ''<?php if($loggedin) : ?>'' inside JavaScript code in order to display one of the buttons, either push or pop. I want to keep this button hidden from the client side by embedding ...

Display additional tiles within a compact container

I'm attempting to replicate the user interface used by foursquare! Positioning the map slightly off center like they have . I've figured out how to do one part but struggling with the second part. Initially, I loaded the map in a small div (t ...

Obtaining a group object when the property value matches the itemSearch criteria

What is the best way to extract specific objects from a group when one of their properties has an array value, specifically using _.lodash/underscore? { "tileRecords" : [ { "tileName" : "Fama Brown", "tileGroup" : ["Polished", "Matt", ...

Node.js and EJS are throwing an error, indicating that the file.ejs is unable to identify the variable definitions

I am facing an issue with my express.js server template. A 'x is not defined' error keeps popping up, but I can't seem to find where the problem lies. I even checked a friend's code from the same tutorial, and it works on his machine. ...

Delete the last TD before adding the new element

Below is a snippet of jQuery code that I am working with: html += '<tr>'; html += '<td>AAA</td>'; html += '<td>BBB</td>'; html += '<td>CCC</td>'; html += '<td>DDD ...

In NextJS, where is the best place to run sensitive code to ensure it is only executed server-side?

I'm currently exploring the world of NextJS and I am in the process of figuring out how to structure my project with a solid architecture that prioritizes security. However, I find myself at a crossroads when it comes to determining the best place to ...

Capture a snapshot of a Bootstrap modal using JavaScript

var takeScreenShot = function(){ html2canvas(document.body, { onrendered: function(canvas) { var tempcanvas=document.createElement('canvas'); tempcanvas.width=1350; tempcanvas.height=1350; var context=tempcan ...

A compilation of approved classes for the quill toolbar

The instructions provided in the documentation for the quill module indicate that you can manually create a toolbar using HTML, and then pass the corresponding DOM element or selector to Quill. This will result in the ql-toolbar class being added to the to ...

How does Backbone.js tackle error handling when receiving messages from Rails?

Can anyone using backbone.js lend a hand? How can error messages from a rails app be encoded when integrating with backbone.js? For instance, how should flash messages like "record not found" be handled? While errors are usually defined on the client sid ...