sveltejs / kit

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

[Sveltekit v next.175+ with Netlify adapter] Cannot find module 'stream/package.json' #2523

Closed f-elix closed 2 years ago

f-elix commented 2 years ago

Describe the bug

Builds now fail on Netlify since version next.175. Local builds still work, and Netlify builds work with version next.174. I have another Sveltekit project that runs on Vercel and I have no build problems there.

I would like to provide more detail but I have no idea what's going on here. I provided the error log below.

Reproduction

This is a reproduction repo: https://github.com/f-elix/sveltekit-netlify-build-fail

It's essentially the Sveltekit demo app with netlify-adapter. I tried to deploy it on Netlify and got the same error as my main project.

Logs

11:36:32 AM: ────────────────────────────────────────────────────────────────
11:36:32 AM:   Dependencies installation error                               
11:36:32 AM: ────────────────────────────────────────────────────────────────
11:36:32 AM: ​
11:36:32 AM:   Error message
11:36:32 AM:   A Netlify Function failed to require one of its dependencies.
11:36:32 AM:   Please make sure it is present in the site's top-level "package.json".
​
11:36:32 AM:   In file "/opt/build/repo/.netlify/functions-internal/__render.js"
11:36:32 AM:   Cannot find module 'stream/package.json'
11:36:32 AM:   Require stack:
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/resolve.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/index.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/src/plugins_core/functions/index.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/src/commands/get.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/src/core/main.js
11:36:32 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.22.6/lib/node_modules/@netlify/build/src/core/bin.js
11:36:32 AM: ​

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 24.27 GB / 39.76 GB
  Binaries:
    Node: 14.15.5 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
    npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (94.0.992.31)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    @sveltejs/adapter-netlify: ^1.0.0-next.31 => 1.0.0-next.31
    @sveltejs/kit: ^1.0.0-next.175 => 1.0.0-next.175
    svelte: ^3.43.0 => 3.43.0

Severity

blocking an upgrade

Additional Information

No response

tomatrow commented 2 years ago

Hi,

I had this problem too, npm i -D stream should fix it.

I think the last time I encountered this, it was because of hidden dependencies in node-fetch.

e.g. #1925

rchrdnsh commented 2 years ago

I had the same issue and installing stream fixed it for me as well...

we will need to make a note of this in the docs for the netlify adapter, no?

Or is this something that can be fixed in kit?

benmccann commented 2 years ago

This may be related to https://github.com/rollup/plugins/issues/1004, so perhaps this will be fixed when https://github.com/rollup/plugins/pull/1005 is merged and then Vite is upgraded to use it

niwasmala commented 2 years ago

Adding npm i -D stream does solve the problem in netlify build.

But when I use SSR and access my application, got

{
  "errorType": "ReferenceError",
  "errorMessage": "fetch is not defined",
  "trace": [
    "ReferenceError: fetch is not defined",
    "    at get_hooks (/var/task/functions/render/index.js:31627:41)",
    "    at init (/var/task/functions/render/index.js:31503:17)",
    "    at Object.<anonymous> (/var/task/functions/render/index.js:33889:1)",
    "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
    "    at Module.load (internal/modules/cjs/loader.js:863:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
    "    at Module.require (internal/modules/cjs/loader.js:887:19)",
    "    at require (internal/modules/cjs/helpers.js:74:18)",
    "    at Object.<anonymous> (/var/task/render.js:1:18)"
  ]
}

image

Using @sveltejs/kit@1.0.0-next.176 and svelte@3.43.0.

Any help? Thank you.

dominikg commented 2 years ago

if possible try to update your netlify node version to 16, where stream/web is available

f-elix commented 2 years ago

@tomatrow Installing stream fixed it!

I'd rather not have this in my dependencies but it's not that big of a problem if it makes things work.

Thanks to all for the answers!

benmccann commented 2 years ago

I got a fix into @rollup/plugin-commonjs and upgraded @sveltejs/kit and vite to use it. Can you try upgrading to 1.0.0-next.180 which I just released to see if it helps?

bekos commented 2 years ago

@benmccann Still happening for me with 1.0.0-next.180.

JohnRSim commented 2 years ago

if possible try to update your netlify node version to 16, where stream/web is available

added

  "engines": {
    "node": ">=16"
  }

image Failed on node16 on netlify.

JohnRSim commented 2 years ago

Also tried 1.0.0-next.180 still fails - installing stream for now.

justingolden21 commented 2 years ago

Has there been any progress on this bug?

justingolden21 commented 2 years ago

Got it working with the following netlify.toml

[build]
  command = "npm run build"
  publish = "build/publish/"
  functions = "build/functions/"
  environment = { NODE_VERSION = "14.17.5" }

[dev]
 command = "svelte-kit dev"

[functions]
  directory = "netlify/functions"
  node_bundler = "esbuild"

After a few hours of research and struggling, and a fair number of failed attempts.

Docs: https://docs.netlify.com/configure-builds/file-based-configuration/

borntofrappe commented 2 years ago

What worked for me was specifying the node_bundler to esbuild, in place of the default value of zisi documented for Netlify functions.

[functions]
    node_bundler = "esbuild"

The entire config file consider the default values for the build command and node version 14.18.0.

[build]
  command = "npm run build"
  publish = "build/"

[context.production]
  environment = { NODE_VERSION = "14.18.0" }

[functions]
    node_bundler = "esbuild"

Thank you @justingolden21 for the example 👍

Update: I'm using the kit in version 1.0.0-next.180

borntofrappe commented 2 years ago

Perhaps the issue should be re-opened @benmccann? Sorry to bother you specifically, but you seem the most relevant person.

renefournier commented 2 years ago

In case anyone else runs into trouble deploying Kit to Netlify, the source of my mysterious show-stopping deploy error missing stream.json:

The problem was with the netlify.toml file I was using.

Old, bad, copy-pasted from outdated tutorial (creates stream error)

[build]
  command = "npm run build"
  publish = "build/"
  functions = "functions/"

New, works

[build]
  command = "npm run build"
  publish = "build"

[dev]
 command = "svelte-kit dev"

[functions]
  directory = "netlify/functions"
  node_bundler = "esbuild"

Works!

benmccann commented 2 years ago

175 is the version that upgrades to node-fetch 3.0 final, which pulls in fetch-blob, relying on stream. https://github.com/sveltejs/kit/blob/master/packages/kit/CHANGELOG.md#patch-changes-7

benmccann commented 2 years ago

Here's the contents of the relevant section of .netlify/functions-internal/__render.js

var init_install_fetch = __esm({
  "node_modules/@sveltejs/kit/dist/install-fetch.js"() {
    init_shims();
    import_http = __toModule(require("http"));
    import_https = __toModule(require("https"));
    import_zlib = __toModule(require("zlib"));
    import_stream = __toModule(require("stream"));
    import_util = __toModule(require("util"));
    import_crypto = __toModule(require("crypto"));
    import_url = __toModule(require("url"));
    commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
    src = dataUriToBuffer;
    dataUriToBuffer$1 = src;
    ponyfill_es2018 = { exports: {} };

It seems like a bug on Netlify's side that upgrading to Node 16 doesn't fix this because stream is provided by the Node API there: https://nodejs.org/api/stream.html#stream_stream

I filed a bug with them: https://github.com/netlify/zip-it-and-ship-it/issues/743

benmccann commented 2 years ago

Hmm. I guess my fix to @rollup/plugin-commonjs didn't work in this particular scenario as it did in some others because it's esbuild that's hoisting the import this time around. If we're able to turn the adapters into Vite plugin and get rid of esbuild that possibly would fix this. Though I'm not sure exactly what Netlify is trying to do, so there's a pretty decent chance it'd still fail even if we took esbuild out of the loop. This definitely seems like a Netlify bug, so hopefully they'll fix it: https://github.com/netlify/zip-it-and-ship-it/issues/743

renefournier commented 2 years ago

Hmm, tricky. Jamstack, much as I love it, seems to yield some brittleness in the deploy part.

benmccann commented 2 years ago

There's a PR out that would fix this issue: https://github.com/dependents/node-precinct/pull/88

benmccann commented 2 years ago

This is now working. Existing projects will need to "clear cache and deploy"

samheyman commented 2 years ago

Still getting the error with Node 16, Latest SvelteKit, added stream manually (v0.02), added netlify.toml. Just get the error

Error message
9:08:52 AM:   A Netlify Function failed to require one of its dependencies.
9:08:52 AM:   Please make sure it is present in the site's top-level "package.json".
​
9:08:52 AM:   In file "/opt/build/repo/.netlify/functions-internal/render.js"
9:08:52 AM:   Cannot find module './llhttp/llhttp_simd.wasm.js' from '/opt/build/repo/.netlify'

Was so excited about trying SvelteKit but not being able to rollout to Netlify is a pain.

Dabugger commented 2 years ago

Still getting the error with Node 16, Latest SvelteKit, added stream manually (v0.02), added netlify.toml. Just get the error

Error message
9:08:52 AM:   A Netlify Function failed to require one of its dependencies.
9:08:52 AM:   Please make sure it is present in the site's top-level "package.json".
​
9:08:52 AM:   In file "/opt/build/repo/.netlify/functions-internal/render.js"
9:08:52 AM:   Cannot find module './llhttp/llhttp_simd.wasm.js' from '/opt/build/repo/.netlify'

Was so excited about trying SvelteKit but not being able to rollout to Netlify is a pain.

I'm also having the same issue, started today.

benmccann commented 2 years ago

There's an open issue for that: https://github.com/sveltejs/kit/issues/5337