Next.js: How to get static assets from within getStaticProps

This is a known issue in Next.js (see related discussion #32236), __dirname incorrectly resolves to / – you should use process.cwd() instead.

From the Next.js Reading files documentation:

Files can be read directly from the filesystem in getStaticProps.

In order to do so you have to get the full path to a file.

Since Next.js compiles your code into a separate directory you can’t
use __dirname as the path it will return will be different from the
pages directory.

Instead you can use process.cwd() which gives you the directory where
Next.js is being executed.

Leave a Comment