Skip to content

__dirname in getStaticProps is not reflective of where file actually is #10943

@SivanMehta

Description

@SivanMehta

Bug report

When attempting to reference the local file system, I often use __dirname to ensure that I can use relative path names. However while using getStaticProps, __dirname is not resolving correctly.

Describe the bug

I expected __dirname to be the absolute path of the file in which getStaticProps is being executed, but instead it is incorrectly resolving as /.

To Reproduce

I am using this component in the pages directory under the name faq.js:

import React from 'react';

export default function FAQ ({ faq }) {
  return (
    <pre >
      { faq }
    </pre>
  )
}

export async function getStaticProps() {
  const read = require('util').promisify(require('fs').readFile);
  const path = require('path');

  console.log(__dirname);

  const faq = await read(path.join(__dirname, 'faq.md'), 'utf8');
  return { faq };
};

Expected behavior

I expect __dirname to be the relative path name of faq.js, but it instead is output as / in the console during next build. This is causing the file faq.md to not be read properly because it is not on the correct path.

System information

  • OS: MacOS
  • Version of Next.js: 9.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions