BearToCode / carta

A lightweight, fast and extensible Svelte Markdown editor and viewer.
https://beartocode.github.io/carta/
MIT License
452 stars 20 forks source link

Cannot build on cloudflare #121

Open Pasithea0 opened 1 month ago

Pasithea0 commented 1 month ago

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 | .../esbuild@0.19.12/node_modules/esbuild postinstall$ node install.js 19:23:52.949 | .../esbuild@0.19.12/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/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1651:15) 19:23:54.149 | at /opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1059:25 19:23:54.150 | at /opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1004:52 19:23:54.150 | at buildResponseToResult (/opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1057:7) 19:23:54.150 | at /opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:1086:16 19:23:54.150 | at responseCallbacks. (/opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:704:9) 19:23:54.150 | at handleIncomingPacket (/opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/node_modules/esbuild/lib/main.js:764:9) 19:23:54.150 | at Socket.readFromStdout (/opt/buildhome/repo/node_modules/.pnpm/esbuild@0.19.12/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.

itzCozi commented 1 month ago

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.

BearToCode commented 1 month ago

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.

Pasithea0 commented 1 month ago

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

BearToCode commented 1 month ago

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.

Pasithea0 commented 4 weeks ago

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

BearToCode commented 4 weeks ago

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.

Pasithea0 commented 4 weeks ago

_page.js.txt

BearToCode commented 4 weeks ago

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.

itzCozi commented 4 weeks ago

how can this be fixed?

BearToCode commented 4 weeks ago

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.

Pasithea0 commented 4 weeks ago

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