Invocation Failed: Function crash after deploying Astro.js site - require() of ES module /var/task/.netlify/functions-internal/entry.js not supported

I’m having issues after deploying my Astro site. I’m using the official Netlify adapter for Astro for SSR.
The site works during development but the errors suggest it has something to do with ES modules / Common JS modules.

Site name: https://es-sale.netlify.app/
Site ID: a556a44d-4591-4c01-baa0-ba1de15468da

Having the same issues as this post: [Bug]: ESM not supported with Netlify functions? (part 2) · Issue #1456 · netlify/next-runtime · GitHub
They are configuring their NODE_VERSION and AWS_LAMBDA_JS_RUNTIME in their netlify.toml build environment but mentioned that it didn’t work. I’m using node 16.13.0.

An unhandled error in the function code triggered the following message:

Error - require() of ES Module /var/task/node_modules/astro/dist/core/app/index.js from /var/task/.netlify/functions-internal/entry.js not supported. Instead change the require of index.js in /var/task/.netlify/functions-internal/entry.js to a dynamic import() which is available in all CommonJS modules.

Stack Trace:

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/node_modules/astro/dist/core/app/index.js from /var/task/.netlify/functions-internal/entry.js not supported.
Instead change the require of index.js in /var/task/.netlify/functions-internal/entry.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/var/task/.netlify/functions-internal/entry.js:43036:18)
    at Object.<anonymous> (/var/task/entry.js:1:18)
    at _tryRequireFile (file:///var/runtime/index.mjs:912:37)
    at _tryRequire (file:///var/runtime/index.mjs:962:25)
    at _loadUserApp (file:///var/runtime/index.mjs:991:22)
    at UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:27)
    at start (file:///var/runtime/index.mjs:1192:42)
    at file:///var/runtime/index.mjs:1198:7
  • I’m using “type”: “module” in package.json
  • I’ve tried searching through the node_modules/astro/dist/core/app/index.js file but did not find any require() statements and am unable to access .netlify/functions-internal/entry.js as it doesn’t exist in my project folders.
  • Tried adding node_bundler = “esbuild” to the netlify.toml file which currently looks like this:
[build]
  command = "npm run build"
  publish = "dist"

[functions]
  external_node_modules = ["astro"]
  node_bundler = "esbuild"

Could someone from netlify please help me with this?

There’s no Netlify adapter for Astro, it’s the Astro adapter for Netlify. Please file an issue for the Astro team to investigate. They maintain their integration and they’re the ones who can help you out.

On a related note, I don’t think this would be required:

Thanks for getting taking the time to look at the issue and getting back to me hrishikesh. Sorry I did mean to say the Astro adapter, not the Netlify adapter. You helped point me in the right direction and I have managed to fix this by finding this issue on github: 🐛 BUG: Astro is built fine but crash at runtime when using Netlify ssr adapter · Issue #3984 · withastro/astro · GitHub

Removing the external_node_modules = [“astro”] line in the netlify.toml file fixed the error for me.

Thank you!