javascriptnode.jssveltesveltekitcloudflare-pages

SvelteKit: Packages not being able to access node functions


I am building an app with SvelteKit and publishing it to Cloudflare Pages, but it is some packages I am using are not able to access node native functions. Here's the build log:

2022-07-30T19:05:14.200499Z > Using @sveltejs/adapter-cloudflare
2022-07-30T19:05:14.227308Z ✘ [ERROR] Could not resolve "url"
2022-07-30T19:05:14.227701Z 
2022-07-30T19:05:14.227859Z     node_modules/sequelize/lib/sequelize.js:21:20:
2022-07-30T19:05:14.228009Z       21 │ const url = require("url");
2022-07-30T19:05:14.22817Z           ╵                     ~~~~~
2022-07-30T19:05:14.228388Z 
2022-07-30T19:05:14.228525Z   The package "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.
2022-07-30T19:05:14.228694Z 
2022-07-30T19:05:14.22885Z  ✘ [ERROR] Could not resolve "path"
2022-07-30T19:05:14.229004Z 
2022-07-30T19:05:14.229168Z     node_modules/sequelize/lib/sequelize.js:22:21:
2022-07-30T19:05:14.22932Z        22 │ const path = require("path");
2022-07-30T19:05:14.229452Z          ╵                      ~~~~~~
2022-07-30T19:05:14.229593Z 
2022-07-30T19:05:14.229733Z   The package "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.
2022-07-30T19:05:14.229891Z 
2022-07-30T19:05:14.241501Z ✘ [ERROR] Could not resolve "@firebase/database-compat/standalone"
2022-07-30T19:05:14.2417Z   
2022-07-30T19:05:14.241833Z     node_modules/firebase-admin/lib/app/firebase-namespace.js:106:41:
2022-07-30T19:05:14.242066Z       106 │ ...ject.assign(fn, require('@firebase/database-compat/standalone'));
2022-07-30T19:05:14.242206Z           ╵                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-30T19:05:14.242323Z 
2022-07-30T19:05:14.242435Z   The path "./standalone" is not currently exported by package "@firebase/database-compat":
2022-07-30T19:05:14.242596Z 
2022-07-30T19:05:14.243619Z     node_modules/@firebase/database-compat/package.json:16:13:
2022-07-30T19:05:14.244219Z       16 │   "exports": {
2022-07-30T19:05:14.244437Z          ╵              ^
2022-07-30T19:05:14.244574Z 
2022-07-30T19:05:14.244698Z   None of the conditions provided ("types", "node") match any of the currently active conditions ("browser", "default", "require"):
2022-07-30T19:05:14.24483Z  
2022-07-30T19:05:14.244956Z     node_modules/@firebase/database-compat/package.json:27:20:
2022-07-30T19:05:14.24507Z        27 │     "./standalone": {
2022-07-30T19:05:14.245196Z          ╵                     ^
2022-07-30T19:05:14.245368Z 
2022-07-30T19:05:14.2455Z     Consider enabling the "types" condition if this package expects it to be enabled. You can use "conditions: ['types']" to do that:
2022-07-30T19:05:14.245626Z 
2022-07-30T19:05:14.245736Z     node_modules/@firebase/database-compat/package.json:28:6:
2022-07-30T19:05:14.245847Z       28 │       "types": "./dist/database-compat/src/index.standalone.d.ts",
2022-07-30T19:05:14.245969Z          ╵       ~~~~~~~
2022-07-30T19:05:14.24608Z  
2022-07-30T19:05:14.246397Z   You can mark the path "@firebase/database-compat/standalone" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
2022-07-30T19:05:14.246547Z 
2022-07-30T19:05:14.282052Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.282324Z 
2022-07-30T19:05:14.282497Z     node_modules/retry-as-promised/index.js:3:19:
2022-07-30T19:05:14.282669Z       3 │ var util = require('util');
2022-07-30T19:05:14.282834Z         ╵                    ~~~~~~
2022-07-30T19:05:14.282992Z 
2022-07-30T19:05:14.28312Z    The package "util" 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.
2022-07-30T19:05:14.283505Z 
2022-07-30T19:05:14.317451Z ✘ [ERROR] Could not resolve "url"
2022-07-30T19:05:14.317714Z 
2022-07-30T19:05:14.317856Z     node_modules/pg-connection-string/index.js:3:18:
2022-07-30T19:05:14.317986Z       3 │ var url = require('url')
2022-07-30T19:05:14.318109Z         ╵                   ~~~~~
2022-07-30T19:05:14.318238Z 
2022-07-30T19:05:14.318505Z   The package "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.
2022-07-30T19:05:14.318665Z 
2022-07-30T19:05:14.319392Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.319808Z 
2022-07-30T19:05:14.319968Z     node_modules/sequelize/lib/utils/deprecations.js:32:37:
2022-07-30T19:05:14.320241Z       32 │ var import_util = __toModule(require("util"));
2022-07-30T19:05:14.320559Z          ╵                                      ~~~~~~
2022-07-30T19:05:14.320699Z 
2022-07-30T19:05:14.320823Z   The package "util" 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.
2022-07-30T19:05:14.321042Z 
2022-07-30T19:05:14.326057Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.327248Z 
2022-07-30T19:05:14.328892Z     node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:2:19:
2022-07-30T19:05:14.329459Z       2 │ const fs = require("fs");
2022-07-30T19:05:14.329823Z         ╵                    ~~~~
2022-07-30T19:05:14.33005Z  
2022-07-30T19:05:14.330876Z   The package "fs" 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.
2022-07-30T19:05:14.331054Z 
2022-07-30T19:05:14.331281Z ✘ [ERROR] Could not resolve "assert"
2022-07-30T19:05:14.331476Z 
2022-07-30T19:05:14.331604Z     node_modules/sequelize/lib/model.js:21:23:
2022-07-30T19:05:14.33172Z        21 │ const assert = require("assert");
2022-07-30T19:05:14.331829Z          ╵                        ~~~~~~~~
2022-07-30T19:05:14.331936Z 
2022-07-30T19:05:14.33237Z    The package "assert" 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.
2022-07-30T19:05:14.332543Z 
2022-07-30T19:05:14.332673Z ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.332789Z 
2022-07-30T19:05:14.333338Z     node_modules/sequelize/lib/data-types.js:2:21:
2022-07-30T19:05:14.334026Z       2 │ const util = require("util");
2022-07-30T19:05:14.334206Z         ╵                      ~~~~~~
2022-07-30T19:05:14.33441Z  
2022-07-30T19:05:14.334627Z   The package "util" 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.
2022-07-30T19:05:14.334959Z 
2022-07-30T19:05:14.335106Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.335239Z 
2022-07-30T19:05:14.33556Z      node_modules/pg-connection-string/index.js:4:17:
2022-07-30T19:05:14.335971Z       4 │ var fs = require('fs')
2022-07-30T19:05:14.336165Z         ╵                  ~~~~
2022-07-30T19:05:14.336703Z 
2022-07-30T19:05:14.337207Z   The package "fs" 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.
2022-07-30T19:05:14.337373Z 
2022-07-30T19:05:14.337521Z ✘ [ERROR] Could not resolve "fs"
2022-07-30T19:05:14.337657Z 
2022-07-30T19:05:14.337784Z     node_modules/firebase-admin/lib/app/lifecycle.js:21:19:
2022-07-30T19:05:14.337911Z       21 │ const fs = require("fs");
2022-07-30T19:05:14.338056Z          ╵                    ~~~~
2022-07-30T19:05:14.338211Z 
2022-07-30T19:05:14.338342Z   The package "fs" 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.
2022-07-30T19:05:14.338478Z 
2022-07-30T19:05:14.33862Z  ✘ [ERROR] Could not resolve "util"
2022-07-30T19:05:14.338756Z 
2022-07-30T19:05:14.339295Z     node_modules/sequelize/lib/utils/logger.js:59:37:
2022-07-30T19:05:14.339765Z       59 │ var import_util = __toModule(require("util"));
2022-07-30T19:05:14.339936Z          ╵                                      ~~~~~~
2022-07-30T19:05:14.340093Z 
2022-07-30T19:05:14.340237Z   The package "util" 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.


...............


2022-07-30T19:05:14.89587Z  
2022-07-30T19:05:14.896004Z error during build:
2022-07-30T19:05:14.896124Z Error: Build failed with 234 errors:
2022-07-30T19:05:14.896252Z node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js:1:31: ERROR: Could not resolve "stream"
2022-07-30T19:05:14.896371Z node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js:2:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896477Z node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js:1:29: ERROR: Could not resolve "events"
2022-07-30T19:05:14.896588Z node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js:2:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896701Z node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js:1:25: ERROR: Could not resolve "util"
2022-07-30T19:05:14.896818Z ...
2022-07-30T19:05:14.896942Z     at failureErrorWithLog (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1621:15)
2022-07-30T19:05:14.897064Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:1263:28
2022-07-30T19:05:14.897441Z     at runOnEndCallbacks (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1176:65)
2022-07-30T19:05:14.897756Z     at buildResponseToResult (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:1261:7)
2022-07-30T19:05:14.898025Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:1374:14
2022-07-30T19:05:14.898328Z     at /opt/buildhome/repo/node_modules/esbuild/lib/main.js:675:9
2022-07-30T19:05:14.898627Z     at handleIncomingPacket (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:772:9)
2022-07-30T19:05:14.898889Z     at Socket.readFromStdout (/opt/buildhome/repo/node_modules/esbuild/lib/main.js:641:7)
2022-07-30T19:05:14.90122Z      at Socket.emit (node:events:527:28)
2022-07-30T19:05:14.901427Z     at addChunk (node:internal/streams/readable:315:12)
2022-07-30T19:05:14.917314Z Failed: build command exited with code: 1
2022-07-30T19:05:16.243508Z Failed: an internal error occurred

This is happening just after I added some server side code so that might be the reason, but Sveltekit supports that through cloudflare workers so I don't understand why.


Solution

  • I ran into this same issue trying to migrate from Vercel to Cloudflare Pages and it looks like Cloudflare Workers (which is what Pages uses under the hood for functions) only supports Node.js packages that uses webpack or another polyfill bundler since it is not Node.js. It only runs JavaScript code using the V8 engine on the edge network not in a Node.js runtime so any Node.js package that has Node.js specific dependencies like fs and net/http are not compatible.

    Here is a list of Cloudflare Workers compatible packages and here is some further reading on how Node.js and Cloudflare Workers work together.

    To get your project deployed to Cloudflare Pages you will need to refactor your code to only use Node.js packages from the above list of compatible packages or implement any functionality using plain JavaScript if a compatible package does not exist.