Next.js API handler - Cannot find module '/var/task/.next/server/pages/_document.js'

Site name: https://homage-production.netlify.app/

Hello!

I am attempting to implement on demand revalidation for some static pages on my site. Everything is working locally in production mode but the webhook I have setup with my CMS receives a 500 response from my API route hosted on Netlify. When checking my function details the Next.js API handler is showing this error:


Jul 3, 12:54:08 PM: d2c67316 ERROR  Error: Cannot find module '/var/task/.next/server/pages/_document.js'
Require stack:
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-api-handler/___netlify-api-handler.js
- /var/task/___netlify-api-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at mod._resolveFilename (/var/task/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.requirePage (/var/task/node_modules/next/dist/server/require.js:88:12)
    at /var/task/node_modules/next/dist/server/load-components.js:45:54
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async Object.loadComponentsImpl [as loadComponents] (/var/task/node_modules/next/dist/server/load-components.js:44:33) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-api-handler/___netlify-api-handler.js',
    '/var/task/___netlify-api-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Jul 3, 12:54:08 PM: d2c67316 ERROR  Error: Cannot find module '/var/task/.next/server/pages/_document.js'
Require stack:
- /var/task/node_modules/next/dist/server/require.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/.netlify/functions-internal/___netlify-api-handler/___netlify-api-handler.js
- /var/task/___netlify-api-handler.js
- /var/runtime/index.mjs
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at mod._resolveFilename (/var/task/node_modules/next/dist/build/webpack/require-hook.js:23:32)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.requirePage (/var/task/node_modules/next/dist/server/require.js:88:12)
    at /var/task/node_modules/next/dist/server/load-components.js:45:54
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Promise.all (index 0)
    at async Object.loadComponentsImpl [as loadComponents] (/var/task/node_modules/next/dist/server/load-components.js:44:33) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/next/dist/server/require.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/.netlify/functions-internal/___netlify-api-handler/___netlify-api-handler.js',
    '/var/task/___netlify-api-handler.js',
    '/var/runtime/index.mjs'
  ]
}
Jul 3, 12:54:08 PM: d2c67316 INFO   [POST] /api/revalidate (API)
Jul 3, 12:54:08 PM: d2c67316 Duration: 6.31 ms	Memory Usage: 100 MB

When I build locally I can find this file at “web/.next/server/pages/_document.js”. I downloaded my assets from my most recent build on Netlify but could not find a server folder. My netlify.toml is setup like so:

[[plugins]]
  package = "@netlify/plugin-nextjs"

but in my build configuration I have my base directory set to “web”, my build command set to “next build” and my publish directory set to “web/.next”.

I haven’t found a lot of information about why my function might be generating this error and am sort of lost as to what the problem may be. Would really appreciate if anyone could point me in the right direction! Thank you.

hi @clarity, we have just rolled out a fix for that specific issue - it was yesterday, so you might need to rebuild your site. however, once that is fixed you will definitely run into another issue because on demand revalidation is not currently supported. it is an ongoing project to implement this and it won’t be long before we can release it, but you’ll have to hang on a little longer unfortunately.

Hello @orinokai!

Good to hear about the fix.

I’m seeing now in your docs that you don’t support On Demand Revalidation. I’m finding some older posts about the efficacy of ISR on Netlify and I would love your input.

I’m mixing SSR with SSG for my small e-commerce site (3-5k products). Based on the info provided I could either implement ISR on my static pages or trigger a rebuild anytime I make changes in the CMS and grab fresh data client side when necessary. I was hoping to avoid rebuilding for every update. Any thoughts?

Thanks for your help :slight_smile:

Sure, it depends on how fresh the content needs to be and how much traffic you get on those static pages. If you don’t mind the content being stale for a few hours and you get enough traffic to trigger an update within that time you could use ISR. If not, and you want to avoid triggering a full rebuild, the only other options are server-side rendering or client-side rendering, until we can release the on-demand revalidation feature. Hope that helps!