Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudflare adapter: Could not resolve "path" and "fs" #2850

Closed
apvarun opened this issue Nov 21, 2021 · 6 comments
Closed

Cloudflare adapter: Could not resolve "path" and "fs" #2850

apvarun opened this issue Nov 21, 2021 · 6 comments

Comments

@apvarun
Copy link

apvarun commented Nov 21, 2021

Describe the bug

For deploying a markdown blog using sveltekit to cloudflare, it is required to read the file system to get the list of files. This fails after adding the cloudflare adapter

> Build failed with 11 errors:
.svelte-kit/output/server/chunks/_slug_.json-8564ef2a.js:1:17: error: Could not resolve "path" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/_slug_.json-8564ef2a.js:3:7: error: Could not resolve "fs" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/blog-5b3957a8.js:1:15: error: Could not resolve "fs" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/index.json-5265f1f0.js:1:17: error: Could not resolve "path" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/index.json-5265f1f0.js:3:7: error: Could not resolve "fs" (use "platform: 'node'" when building for node)

Reproduction

Clone Repo: https://github.com/rodneylab/sveltekit-blog-mdx
Run: npm i --save-dev @sveltejs/adapter-cloudflare@next
Change the adapter in svelte.config.js to cloudflare-adapter
Run npm run build

Logs

Error: Build failed with 11 errors:
.svelte-kit/output/server/chunks/_slug_.json-8564ef2a.js:1:17: error: Could not resolve "path" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/_slug_.json-8564ef2a.js:3:7: error: Could not resolve "fs" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/blog-5b3957a8.js:1:15: error: Could not resolve "fs" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/index.json-5265f1f0.js:1:17: error: Could not resolve "path" (use "platform: 'node'" when building for node)
.svelte-kit/output/server/chunks/index.json-5265f1f0.js:3:7: error: Could not resolve "fs" (use "platform: 'node'" when building for node)
...
    at failureErrorWithLog (/private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:1493:15)
    at /private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:1151:28
    at runOnEndCallbacks (/private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:1069:65)
    at buildResponseToResult (/private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:1149:7)
    at /private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:1258:14
    at /private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/private/tmp/sveltekit-blog-mdx/node_modules/esbuild/lib/main.js:596:7)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
error Command failed with exit code 1.

System Info

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 46.63 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.4 - ~/.nvm/versions/node/v14.17.4/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 7.23.0 - ~/.nvm/versions/node/v14.17.4/bin/npm
  Browsers:
    Brave Browser: 95.1.31.88
    Chrome: 96.0.4664.55
    Firefox: 93.0
    Safari: 15.1
  npmPackages:
    @sveltejs/adapter-cloudflare: ^1.0.0-next.2 => 1.0.0-next.2
    @sveltejs/adapter-static: ^1.0.0-next.21 => 1.0.0-next.21
    @sveltejs/kit: ^1.0.0-next.182 => 1.0.0-next.196
    svelte: ^3.43.2 => 3.44.2

Severity

blocking all usage of SvelteKit

Additional Information

Prevent the deployment of the project. The build in cloudflare deployment environment also fails for the same reason.

@flayks
Copy link

flayks commented Nov 21, 2021

Same problem here! Haven't got much luck on the Discord so far.

It would be lovely to have API endpoints working with CF pages since (I believe) the Cloudflare blog post says that it should now work with a Node environment.

@apvarun
Copy link
Author

apvarun commented Nov 22, 2021

@flayks I'm able to get it to work after removing all node based dependencies. I was using path and fs packages for generating JSON in API, but then I had to move it to a script in prebuild and then consume it in my app. Not possible to have it dynamic via API routes.

@si3nloong
Copy link
Contributor

Ah, I found the bug. Since the code is using path and fs which is belong to Nodejs API, but the platform was assigned as browser instead node, so esbuild throw errors when it compiling the codes.

platform: 'browser',

I can solve this easily, but I think I need to understand why it is set to browser instead of node in the first place.

@raul-reclique
Copy link

Changing to platform 'node' fixes the building error but still does not deploy to cloudflare. Here is the example, it builds
but it fails "Deploying your site to Cloudflare's global network..."

20:50:15.106 | Run npm run preview to preview your production build locally.
-- | --
20:50:15.114 |  
20:50:15.114 | > Using @sveltejs/adapter-cloudflare
20:50:15.568 | ✔ done
20:50:15.585 | Finished
20:50:15.585 | Note: No functions dir at /functions found. Skipping.
20:50:15.585 | Found _worker.js in output directory. Uploading.
20:50:15.596 | Validating asset output directory
20:50:16.011 | Deploying your site to Cloudflare's global network...
20:50:24.954 | Failed: an internal error occurred

@babichjacob
Copy link
Member

Cloudflare Workers do not run Node. While it looks weird that the platform being built for is browser, that's practically what it is since Cloudflare Workers run "just" V8, which means that the built-in Node libraries are not there. Changing the platform to node won't make Node libraries available on Workers, so you must change your code not to use Node libraries or packages that depend on them.

@Rich-Harris
Copy link
Member

There's a few things that need to happen here to prevent this sort of thing:

Since we have issues tracking all those things, I'll close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants