Gatsby SSR function crash "no such file or directory"

Hi! I’m trying to get an SSR page on Gatsby working. It works locally with netlify dev, but not when deployed. The function crashes with

Error - ENOENT: no such file or directory, lstat '/var/task/.cache/data'

Error: ENOENT: no such file or directory, lstat '/var/task/.cache/data'
    at Object.lstatSync (node:fs:1668:3)
    at Object.lstatSync (/var/task/node_modules/graceful-fs/polyfills.js:318:34)
    at statFunc (/var/task/node_modules/fs-extra/lib/util/stat.js:24:20)
    at getStatsSync (/var/task/node_modules/fs-extra/lib/util/stat.js:25:19)
    at Object.checkPathsSync (/var/task/node_modules/fs-extra/lib/util/stat.js:67:33)
    at Object.copySync (/var/task/node_modules/fs-extra/lib/copy/copy-sync.js:27:38)
    at setupFsWrapper (/var/task/hem-store-us/.cache/page-ssr/lambda.js:64:10)
    at Object.<anonymous> (/var/task/hem-store-us/.cache/page-ssr/lambda.js:70:16)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

It’s a branch deploy (PR). Here’s a link to the deploy in question (although this happens on every deploy I’ve made so far).


  • “gatsby”: “^5.12.4”
  • “gatsby-adapter-netlify”: “^1.0.1”

Any help would be appreciated!

Could it be related to the SSR page is inside a gatsby theme I reuse for multiple pages?

My structure

- my-site/ (the site that Netlify builds, and this site uses my-theme in my yarn monorepo)
- my-theme/
          [orderId].tsx (this is the page that wants to use SSR)

Tested a build where I moved [orderId].tsx to my-site instead, and it didn’t help :slight_smile:

Hiya @davidpaulsson ,

I’d guess that you are trying to somehow write or read local files in the lambda which in general isn’t a typical use pattern as far as I know. Since we mock the lambda in netlify dev it’s not entirely unexpected that it would work differently locally than in the cloud.

I wonder if you could update your code to use /tmp for this file/directory, instead of /var/<anything> as that appears to be a better-suited location for writing things to the “local filesystem” in a lambda. I’m not a lambda user myself so please take this with a grain of salt, as I am only attempting to address the proximate symptom you’ve shown.

Hi! Thanks for replying :slight_smile:

Nope, I’m not trying to either write or read any files. It’s just a fetch request that fetches and returns some JSON, nothing special.

I’ve created a little demo here :arrow_right:

This file looks like this

import * as React from "react"

const SSRPage = ({ serverData }) => (
    <h1>SSR Page with Dogs</h1>
    <img alt="Happy dog" src={serverData.message} />

export default SSRPage

export async function getServerData() {
  try {
    const res = await fetch(``)

    if (!res.ok) {
      throw new Error(`Response failed`)

    return {
      props: await res.json(),
  } catch (error) {
    return {
      status: 500,
      headers: {},
      props: {}

which is taken directly from the Gatsby docs SSR example.

Unfortunately, Gatsby itself uses those paths under-the-hood. This was discovered when investigating another issue (which is highly similar to this), but sadly that’s not fixed yet, so very likely this issue would also just remain at the moment.

So… SSR with Gatsby on Netlify is not a possibility at this time?

Can you try using Gatsby <= 5.11.0?