Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Read A Local File In Nextjs

This is done with `getStaticProps` which happens on the server side

Code

export default function Page({fileAsString}) {
      return <div>{fileAsString}</div>
  }

  export async function getStaticProps(context) {
    try {
      const fileText = fs.readFileSync(
        `./pages/switch-components-via-state.js`,
        `utf8`
      )
      return {
        props: {
          fileAsString: fileText,
        },
      }
    } catch (err) {
      console.log(err)
      return {
        props: {
          fileAsString: `could not find file`,
        },
      }
    }
  }

Note that in the node docs` but here it's done with `import fs from 'fs'`