Sveltekit + Cloudflare Pages + node core modules

Hello, I’m trying to figure out how to move my sveltekit project from vercel to cloudflare pages. It seems that cloudflare pages do not support node core modules, from which I use the path and crypt packages. I tried to google and different methods, but did not get it to work.

Is there any way to fix this issue, I would love to move my page to also be hosted on cloudflare?

You can add the compatibility flag nodejs_compat to add Node APIs - Node.js compatibility · Cloudflare Workers docs

I added the nodejs_compat and changed my imports from ‘path’ and ‘crypto’ to ‘node:path’ and ‘node:crypto’. Still fails.

Just says:

14:07:15.800	> Using @sveltejs/adapter-auto
14:07:17.119	✘ [ERROR] Could not resolve "node:path"
14:07:17.120	
14:07:17.120	    .svelte-kit/output/server/chunks/utils.js:3:17:
14:07:17.120	      3 │ import path from "node:path";
14:07:17.120	        ╵                  ~~~~~~~~~~~
14:07:17.121	
14:07:17.121	  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.
14:07:17.121	
14:07:17.124	✘ [ERROR] Could not resolve "node:crypto"
14:07:17.124	
14:07:17.124	    .svelte-kit/output/server/chunks/utils.js:6:27:
14:07:17.124	      6 │ import { createHash } from "node:crypto";
14:07:17.124	        ╵                            ~~~~~~~~~~~~~
14:07:17.125	
14:07:17.125	  The package "node: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.
14:07:17.125	
14:07:17.352	error during build:
14:07:17.352	Error: Build failed with 2 errors:
14:07:17.353	.svelte-kit/output/server/chunks/utils.js:3:17: ERROR: Could not resolve "node:path"
14:07:17.353	.svelte-kit/output/server/chunks/utils.js:6:27: ERROR: Could not resolve "node:crypto"

This can be pretty easily confirmed by just making an empty sveltekit project:

npm create svelte@latest my-app
cd my-app
npm install

and then adding this file to src/routes/+page.server.ts:


/** @type {import('./$types').PageLoad} */
export function load() {
	return {
		post: path.basename('/test/test.txt')
	};
}

and adding the nodejs_compat to the compatibility flags and then running the deployment.

Sorry the code example was cutout somehow, here’s the real file contents:

import path from 'node:path';

/** @type {import('./$types').PageLoad} */
export function load() {
	return {
		post: path.basename('/test/test.txt')
	};
}

Trying to use the vite-plugin-node-polyfills, it now produces:

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

    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.

nodejs_compat is an experimental feature and from my own experience with trying to make projects work with NodeJS libraries, it’s a headache and you cannot make it work at this point in time.

The problem is on Sveltekit cloudflare adapter bundler that does not recognise “node:” as external prefix. Here is a trivial fix that works for me: https://github.com/sveltejs/kit/pull/11373

1 Like