sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.78k stars 1.96k forks source link

Node Polyfill Build Errors (Adapter-Vercel) #11509

Open solanacryptodev opened 11 months ago

solanacryptodev commented 11 months ago

Describe the bug

Hello, so I'm using Sveltekit to build a web3 application that uses several libraries popular in the crypto space (although, I suspect the issue is due to popular dependencies). I want to use Vercel, but I ran into issues deploying it due to deploy errors. So I decided to build it locally first and have come across the same errors that I saw on Vercel's end in my local terminal. There is another issue that is very similar to this one, but I wanted to create my own with a repoduction. The Vite build process goes along fine until it hits @sveltejs/adapter-vercel

Reproduction

This is a reproduction. Since I believe this is related to one (or several) of the web3 packages I'm using, all I did was clone node.new and pnpm installed the packages that I think triggers this. I also copied over my tsconfig.json, vite.config.ts and svelte.config.

https://stackblitz.com/edit/sveltejs-kit-template-default-ztpuwo?file=package.json

Logs

> Using @sveltejs/adapter-vercel
  Warning: The following modules failed to locate dependencies that may (or may not) be required for your app to work:
  ../../node_modules/node-fetch/lib/index.js
    - encoding
  node_modules/.pnpm/node-fetch@2.7.0/node_modules/node-fetch/lib/index.js
    - encoding
  ../../node_modules/.pnpm/node-fetch@2.7.0/node_modules/node-fetch/lib/index.js
    - encoding
  node_modules/.pnpm/pino@7.11.0/node_modules/pino/lib/tools.js
    - pino-pretty
✘ [ERROR] Could not resolve "crypto"

    node_modules/.pnpm/@toruslabs+eccrypto@2.2.1/node_modules/@toruslabs/eccrypto/dist/browser.js:7:27:
      7 │ const nodeCrypto = require("crypto");
        ╵                            ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "http"

    node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:54:25:
      54 │     const http = require('http');
         ╵                          ~~~~~~

  The package "http" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "https"

    node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:55:26:
      55 │     const https = require('https');
         ╵                           ~~~~~~~

  The package "https" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "zlib"

    node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:56:25:
      56 │     const zlib = require('zlib');
         ╵                          ~~~~~~

  The package "zlib" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

✘ [ERROR] Could not resolve "stream"

    node_modules/.pnpm/cipher-base@1.0.4/node_modules/cipher-base/index.js:2:24:
      2 │ var Transform = require('stream').Transform
        ╵                         ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

error during build:
Error: Build failed with 5 errors:
node_modules/.pnpm/@toruslabs+eccrypto@2.2.1/node_modules/@toruslabs/eccrypto/dist/browser.js:7:27: ERROR: Could not resolve "crypto"
node_modules/.pnpm/cipher-base@1.0.4/node_modules/cipher-base/index.js:2:24: ERROR: Could not resolve "stream"
node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:54:25: ERROR: Could not resolve "http"
node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:55:26: ERROR: Could not resolve "https"
node_modules/.pnpm/micro-ftch@0.3.1/node_modules/micro-ftch/index.js:56:25: ERROR: Could not resolve "zlib"

### System Info

```Shell
System:
    OS: macOS 12.5.1
    CPU: (6) x64 Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
    Memory: 15.25 MB / 8.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.18.1 - /usr/local/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 9.8.1 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  Browsers:
    Brave Browser: 120.1.61.109
    Chrome: 120.0.6099.129
    Safari: 15.6.1
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.1 
    @sveltejs/adapter-vercel: ^3.1.0 => 3.1.0 
    @sveltejs/kit: ^1.27.4 => 1.27.7 
    svelte: ^4.2.7 => 4.2.8 
    vite: ^4.4.2 => 4.5.1

Severity

blocking all usage of SvelteKit

Additional Information

At this moment, this is blocking all builds (deployments) of Sveltekit locally and on Vercel. Localhost works fine though, but this is a pretty significant blocker and prevents me from testing the application itself on a staging site. Only thing I can really do is pivot to other tasks that don't require interacting with the blockchain any. One final thing, when I installed these packages onto StackBlitz, I tested the default code and the project builds fine, after installation of these packages (which work fine with other frameworks like Next.js and I have a project on Vercel with some of the Metaplex libraries being used)..all of a sudden the default StackBlitz project no longer builds. I also noticed it says these packages are deprecated and one of them appears in the error log I posted above for crypto, etc.

deprecated

solanacryptodev commented 11 months ago

Ok, after some troubleshooting...I've narrowed the @sveltejs/adapter-vercel build failure down to these two packages. Problem is, this is app breaking. I can't use Sveltekit if I can't deploy to Vercel with these highly important packages. I use both of these packages in a Next.JS app I built and launched onto Vercel. I've tried using the old versions of both packages that I used in the Next app a few months ago and it still failed. I also switched out @sveltejs/adapter-vercel for @sveltejs/adapter-auto and while it builds locally with auto...it still fails to deploy in Vercel. So that's a false positive.

I'm not entirely sure who to ask for advice for this. The Sveltejs team, the Vite team, or the team that owns these packages. My next step is to whip up a default NextJS app and install these packages and try to deploy to vercel and see if I run into the same error, but given the fact that I did that a few months back and didn't run into this issue, I'm inclined to think the error is either with Svelte/Sveltekit or Vite.

@solana/wallet-adapter-base": "^0.9.23",
@solana/wallet-adapter-wallets": "^0.19.24",
dummdidumm commented 11 months ago

I ran npm run build and pnpm build (after pnpm install) inside the Stackblitz container and both worked fine - probably because you didn't use them in the reproduction repo so they were just omitted. Since you said you narrowed this down, could you provide another Stackblitz link where the build actually fails?