carta icon indicating copy to clipboard operation
carta copied to clipboard

Cannot build on cloudflare

Open Pasithea0 opened this issue 1 year ago • 15 comments

I can't build on cloudflare pages. Using svelte adapter-auto it chooses cloudflare's adapter. However, it does build with adapter-node but it just returns 404. It works fine on vercel.

Here are the relevant logs:

Could not resolve "node:process"... Could not resolve "node:path"... Could not resolve "node:URL"... (Expand for log)
19:23:50.651 | > Using @sveltejs/adapter-auto
-- | --
19:23:50.935 | Installing @sveltejs/adapter-cloudflare...
19:23:51.514 | Progress: resolved 0, reused 1, downloaded 0, added 0
19:23:52.602 | Progress: resolved 325, reused 259, downloaded 6, added 0
19:23:52.605 | Packages: +6
19:23:52.609 | ++++++
19:23:52.754 | Progress: resolved 325, reused 260, downloaded 6, added 6, done
19:23:52.848 | .../[email protected]/node_modules/esbuild postinstall$ node install.js
19:23:52.949 | .../[email protected]/node_modules/esbuild postinstall: Done
19:23:53.251 |  
19:23:53.252 | devDependencies:
19:23:53.252 | + @sveltejs/adapter-cloudflare 3.0.2 (4.7.2 is available)
19:23:53.252 |  
19:23:53.254 | Done in 2.1s
19:23:53.340 | Successfully installed @sveltejs/adapter-cloudflare.
19:23:53.341 |  
19:23:53.341 | If you plan on staying on this deployment platform, consider replacing @sveltejs/adapter-auto with @sveltejs/adapter-cloudflare. This will give you faster and more robust installs, and more control over deployment configuration.
19:23:53.341 |  
19:23:54.019 | ✘ [ERROR] Could not resolve "node:process"
19:23:54.019 |  
19:23:54.019 | .svelte-kit/output/server/chunks/_page.js:2:20:
19:23:54.019 | 2 │ import minproc from "node:process";
19:23:54.019 | ╵                     ~~~~~~~~~~~~~~
19:23:54.019 |  
19:23:54.019 | The package "node:process" 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.
19:23:54.019 |  
19:23:54.020 | ✘ [ERROR] Could not resolve "node:path"
19:23:54.020 |  
19:23:54.020 | .svelte-kit/output/server/chunks/_page.js:3:20:
19:23:54.020 | 3 │ import minpath from "node:path";
19:23:54.020 | ╵                     ~~~~~~~~~~~
19:23:54.020 |  
19:23:54.020 | The package "node:path" 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.
19:23:54.020 |  
19:23:54.021 | ✘ [ERROR] Could not resolve "node:url"
19:23:54.021 |  
19:23:54.021 | .svelte-kit/output/server/chunks/_page.js:4:30:
19:23:54.021 | 4 │ import { fileURLToPath } from "node:url";
19:23:54.021 | ╵                               ~~~~~~~~~~
19:23:54.021 |  
19:23:54.021 | The package "node:url" 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.
19:23:54.021 |  
19:23:54.147 | error during build:
19:23:54.148 | Error: Build failed with 3 errors:
19:23:54.148 | .svelte-kit/output/server/chunks/_page.js:2:20: ERROR: Could not resolve "node:process"
19:23:54.149 | .svelte-kit/output/server/chunks/_page.js:3:20: ERROR: Could not resolve "node:path"
19:23:54.149 | .svelte-kit/output/server/chunks/_page.js:4:30: ERROR: Could not resolve "node:url"
19:23:54.149 | at failureErrorWithLog (/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1651:15)
19:23:54.149 | at /opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1059:25
19:23:54.150 | at /opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1004:52
19:23:54.150 | at buildResponseToResult (/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1057:7)
19:23:54.150 | at /opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1086:16
19:23:54.150 | at responseCallbacks.<computed> (/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:704:9)
19:23:54.150 | at handleIncomingPacket (/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:764:9)
19:23:54.150 | at Socket.readFromStdout (/opt/buildhome/repo/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:680:7)
19:23:54.151 | at Socket.emit (node:events:514:28)
19:23:54.151 | at addChunk (node:internal/streams/readable:324:12)
19:23:54.219 | Failed: Error while executing user command. Exited with error code: 1
19:23:54.228 | Failed: build command exited with code: 1
19:23:55.291 | Failed: error occurred while running build command

These packages were not imported by me and it built fine after removing carta.

Pasithea0 avatar Oct 09 '24 01:10 Pasithea0

Glad to see I wasn't the only one with this issue, we have been building it on railway instead sadly, Cloudflare pages is the best host though sad to see this package hinders it. When I build on Cloudflare using the cloudflare-adapter I get the same "Could not resolve" errors.

itzCozi avatar Oct 09 '24 02:10 itzCozi

Hello, I do not have experience using the cloudflare adapter. It seems like when bundling for the client some node packages are still included. I do not recognize any of the imports (minpath, minproc and fileURLToPath): they are probably importated from a dependency of the library. If you are able to determine which one is causing the issue, we might be able to tree-shake a wrongful import.

BearToCode avatar Oct 09 '24 07:10 BearToCode

The issue is with carta. After removing carta package it builds. After adding carta, it fails to build. Everything else builds fine.

Pasithea0 avatar Oct 09 '24 14:10 Pasithea0

I mean that the issue is with a dependency of Carta, not of you project. It's hard to tell without seeing where the problematic imports are used.

BearToCode avatar Oct 09 '24 15:10 BearToCode

Ah gotcha. The only reference to this issue I could find is here:

"Cloudflare has a different runtime than Nodejs so you won't have access to all of the node "things"." https://www.reddit.com/r/sveltejs/comments/17u9p53/sveltekit_cloudflare_pages_node_core_modules/

I just hoped you had a workaround for this issue

Pasithea0 avatar Oct 09 '24 15:10 Pasithea0

Can you provide the .svelte-kit/output/server/chunks/_page.js file where the issue originates? I might be able to figure out what's causing the issue there.

BearToCode avatar Oct 09 '24 15:10 BearToCode

_page.js.txt

Pasithea0 avatar Oct 09 '24 15:10 Pasithea0

Thanks to the file you provided I found the source of the import. It's carta-md -> unified -> vfile.

// vfile/lib/index.js
import {minpath} from '#minpath'
import {minproc} from '#minproc'
import {urlToPath, isUrl} from '#minurl'

(source)

Apparently, there should be a polyfill in place to replace the imports when node is not used. I don't know why it's not working and how it is implemanted.

BearToCode avatar Oct 09 '24 16:10 BearToCode

how can this be fixed?

itzCozi avatar Oct 09 '24 18:10 itzCozi

I'd suggest trying to install vfile separately, using it for some basic task to prevent tree-shaking, and then building using the cloudflare-adapter, to determine whether the issue arises as well. You can then open a dedicated issue on the vfile repository.

I currently do not have the time to look into this myself, but I could implement a possible fix you come up with.

BearToCode avatar Oct 09 '24 19:10 BearToCode

Well, I got it to work using the static adapter. Thanks for the help though!

Pasithea0 avatar Oct 09 '24 21:10 Pasithea0

I'd suggest trying to install vfile separately, using it for some basic task to prevent tree-shaking, and then building using the cloudflare-adapter, to determine whether the issue arises as well. You can then open a dedicated issue on the vfile repository.

I currently do not have the time to look into this myself, but I could implement a possible fix you come up with.

Hi! Could you look into this if you got time?

> pnpm run build

.....logs

> Using @sveltejs/adapter-cloudflare
✘ [ERROR] Could not resolve "node:url"

    .svelte-kit/output/server/chunks/_page.js:6:30:
      6 │ import { fileURLToPath } from "node:url";
        ╵                               ~~~~~~~~~~

  Cannot use "node:url" when deploying to Cloudflare.
.svelte-kit/output/server/chunks/_page.js:6:30

import { fileURLToPath } from "node:url";

^^^ this line is the problem

It's issue with VFIle class, like you mentioned.

On searching vfile repo, this issue https://github.com/vfile/vfile/issues/81 comes up. That issue references another issue which is still open.

Any help would be appreciated!

Thanks

AyushSehrawat avatar Jan 11 '25 18:01 AyushSehrawat

Hi, I'm quite busy at the moment and I don't have much time to look into this. Anyway, I tried to build using @sveltejs/adapter-cloudflare and no error showed up. I might have missed some configuration step. Could you provide a minimal reproducible example where that error shows up? I can't promise anything, but I'll try to check it out it as soon as I get some time.

BearToCode avatar Jan 14 '25 10:01 BearToCode

Hi, I'm quite busy at the moment and I don't have much time to look into this. Anyway, I tried to build using @sveltejs/adapter-cloudflare and no error showed up. I might have missed some configuration step. Could you provide a minimal reproducible example where that error shows up? I can't promise anything, but I'll try to check it out it as soon as I get some time.

I just created a new project to test with pnpx sv create my-app , selected some things including svelte-adapters -> cloudflare-pages adapter

Then added the example from carta site, ran pnpm run build and got the error.

AyushSehrawat avatar Jan 14 '25 16:01 AyushSehrawat

My project with Carta runs on cloudflare with no problems.

I created it using the create-cloudflare tool (and I noticed that what it outputs is very different from what is in the cloudflare docs for svelte/sveltekit)

https://developers.cloudflare.com/pages/framework-guides/deploy-a-svelte-kit-site/#setting-up-a-new-project

colinrobinsonuib avatar May 08 '25 08:05 colinrobinsonuib