In your Netlify build settings add this environment variable:
AWS_LAMBDA_EXEC_WRAPPER=./lambda_start
This is the trick to have something to customize the start of your server while preserving all the goodies of Next.JS and it works because we know Netlify is on Lambda. Having production run on something that is private implementations is not great, I know.
The other insight is we're just copying over tracing.dist.js
while Netlify's plugin is bundling everything else in one file, so we will not have the node_modules
we're requiring on Lambda. Instead of copying over deps of deps in netlify.toml
, I decided to create my own bundle with esbuild
(remember to add esbuild
to your dev deps).
Using a custom server is absolutely a no-go, at that point you might as well just go with Express. Also tried NODE_OPTIONS
with --require
and it was crashing the build on Netlify at various points, because they decided to have the exact same env vars for both build and runtime.
All this would be super easy if instead either Next.JS or Netlify would provide an entrypoint for devs to use.
¯_(ツ)_/¯