sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.43k stars 1.89k forks source link

adapter-netlify - ReferenceError - Headers is not defined #11536

Closed secondl1ght closed 7 months ago

secondl1ght commented 8 months ago

Describe the bug

When deploying to Netlify I am receiving the following error in production:

image

It appears to be coming from this to_request function here: https://github.com/sveltejs/kit/blob/87da73e361070071b01fc459c5639d26baab34f5/packages/adapter-netlify/src/serverless.js#L62

Reproduction

I have a few sites deployed with SvelteKit and Netlify but only one of them is throwing this error for some reason, even though they are all on the same versions. This is the site causing the issue: https://github.com/secondl1ght/portfolio-v2/tree/6299ee1e190ad0f7107f1721bd9686470cba4aca

Logs

No response

System Info

Note: The following is my local env system info and not what is running on the Netlify server.

  System:
    OS: Linux 5.15 Zorin OS 16.3
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 11.04 GB / 31.19 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 10.2.3 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Brave Browser: 120.1.61.114
  npmPackages:
    @sveltejs/adapter-auto: ^3.1.0 => 3.1.0 
    @sveltejs/kit: ^2.0.6 => 2.0.6 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.1 
    svelte: ^4.2.8 => 4.2.8 
    vite: ^5.0.11 => 5.0.11

Severity

blocking all usage of SvelteKit

Additional Information

NOTE: This happened when I upgraded to SK V2 - previously I was not getting this error in prod.

Thanks

Rich-Harris commented 8 months ago

It sounds like your Netlify functions are using an unsupported Node version. Try updating to 18 (here's how)

secondl1ght commented 8 months ago

Hmm interesting, according to those docs the nvmrc file I have in the root of my project should be defining which Node version Netlify uses. I was using lts/* (20) when this error happened but tried changing it to 18.19.0 and the same Headers is not defined error still happens when I visit the URL.

I use the same LTS nvmrc file and SvelteKit for all my projects usually so I wonder why this is the only one having issues... as a workaround I changed my adapter to use adapter-static for this site because it can actually be served as static files for this particular project.

I am still curious about solving this bug but I can close this issue if you would prefer since I have technically found a workaround. Unless you think it's worth going deeper on this?

Rich-Harris commented 8 months ago

It sounds like the nvmrc file isn't being respected, honestly — is there a place where you could log out process.versions.node to see what's actually running?

secondl1ght commented 8 months ago

I added console.log(process.versions.node, 'hello') to my svelte.config.js file and this is the output from the Netlify build logs:

image

It appears that it is actually using 18.19.0 from what I can tell...

Here is a link to my test branch where this is failing in production: https://github.com/secondl1ght/portfolio-v2/tree/test-node (sorry that it's not a minimal reproduction).

And here is a link to the broken branch deploy in production if you are interested in seeing that: https://test-node--secondl1ght.netlify.app/. 😅

Rich-Harris commented 7 months ago

That shows you which version of Node is building your app, but not which version is running your app — I'd expect them to be the same, but it's possible that they're not. Headers is certainly available in 18.19, so either there's a version mismatch or there's a really weird bug in Netlify

secondl1ght commented 7 months ago

I don't believe Netlify gives you access to the terminal that your site is running on to run log commands unfortunately... I did find this post on a forum where a user claims that the version of node used during the build in the deploy logs is the same version that runs in prod, but we can't really be sure without evidence like you say.

I am happy to close this issue now because I found a personal workaround (even though it meant not using adapter-netlify) and I did a search on the open issues in the SK repo and I don't see any other users having the same issue as me. I am guessing there are many SvelteKit users who deploy with Netlify so if this was a common occurrence there would have been more issues opened by now.

Also I am sure you and the Svelte team have more important things to work on than this! LOL Thanks a lot for helping debug this and for all the work you guys put into developing the awesome framework that is Svelte. :)

stephenlrandall commented 7 months ago

Ran into the same issue after updating a bunch of dependencies:

ReferenceError: Headers is not defined
    at to_request (file:///var/task/.netlify/functions-internal/sveltekit-render.mjs:7550:18)
    at Runtime.handler (file:///var/task/.netlify/functions-internal/sveltekit-render.mjs:7523:43)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)

Fixed by updating the node version in the "Dependency management" section, but then also the runtime Node version:

Screenshot 2024-01-28 at 2 00 47 AM