Skip to content

Instantly share code, notes, and snippets.

@mqunell
Last active April 26, 2022 21:45
Show Gist options
  • Save mqunell/e2780d52717d6550996e124cf0739df3 to your computer and use it in GitHub Desktop.
Save mqunell/e2780d52717d6550996e124cf0739df3 to your computer and use it in GitHub Desktop.
Next.js on Vercel: SSR and local files

Next.js and reading from a local JSON file with fs during build time:

Local (dev) Local (prod) Vercel (prod)
getStaticProps
getServerSideProps

Using getServerSideProps and reading from a local JSON file with fs on Vercel causes "500: Internal Server Error" for unknown reasons. To fix this, the JSON file needs to be imported as a module.

//import fs from 'fs';
import { default as users } from '../data/users.json';
export interface User {
id: number;
name: string;
}
export function getUsers(): User[] {
//const users = JSON.parse(fs.readFileSync('data/users.json').toString());
return users;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment