Does not work on the Edge (Cloudflare Workers)
Closed this issue · 9 comments
Hey, thanks for such an amazing package.
I am using Cloudflare Workers to deploy my sveltekit app, but the deployment fails with an error:
edu:deploy: telefunc v0.1.58 shield() generation
edu:deploy: ✓ shield() generated for the telefunction getSchoolInfo() ([...]/school_drawer.telefunc.ts)
edu:deploy:
edu:deploy: Run npm run preview to preview your production build locally.
edu:deploy:
edu:deploy: > Using @sveltejs/adapter-cloudflare-workers
edu:deploy: ✘ [ERROR] Could not resolve "path"
edu:deploy:
edu:deploy: node_modules/telefunc/dist/cjs/node/server/serverConfig.js:4:23:
edu:deploy: 4 │ const path_1 = require("path");
edu:deploy: ╵ ~~~~~~
edu:deploy:
edu:deploy: 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.
edu:deploy:
edu:deploy: ✘ [ERROR] Could not resolve "path"
edu:deploy:
edu:deploy: node_modules/telefunc/dist/cjs/node/server/runTelefunc/loadTelefuncFilesFromConfig.js:5:23:
edu:deploy: 5 │ const path_1 = require("path");
edu:deploy: ╵ ~~~~~~
edu:deploy:
edu:deploy: 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.
edu:deploy:
edu:deploy: ✘ [ERROR] Could not resolve "path"
edu:deploy:
edu:deploy: node_modules/telefunc/dist/cjs/utils/getOutDirs.js:9:39:
edu:deploy: 9 │ const path_1 = __importDefault(require("path"));
edu:deploy: ╵ ~~~~~~
edu:deploy:
edu:deploy: 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.
edu:deploy:
edu:deploy: error during build:
edu:deploy: Error: Build failed with 3 errors:
edu:deploy: node_modules/telefunc/dist/cjs/node/server/runTelefunc/loadTelefuncFilesFromConfig.js:5:23: ERROR: Could not resolve "path"
edu:deploy: node_modules/telefunc/dist/cjs/node/server/serverConfig.js:4:23: ERROR: Could not resolve "path"
edu:deploy: node_modules/telefunc/dist/cjs/utils/getOutDirs.js:9:39: ERROR: Could not resolve "path"
edu:deploy: at failureErrorWithLog (/Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:1649:15)
edu:deploy: at /Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:1058:25
edu:deploy: at /Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:1003:52
edu:deploy: at buildResponseToResult (/Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:1056:7)
edu:deploy: at /Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:1085:16
edu:deploy: at responseCallbacks.<computed> (/Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:703:9)
edu:deploy: at handleIncomingPacket (/Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:762:9)
edu:deploy: at Socket.readFromStdout (/Users/mark/work/turbo_platforms/node_modules/@sveltejs/adapter-cloudflare-workers/node_modules/esbuild/lib/main.js:679:7)
edu:deploy: at Socket.emit (node:events:514:28)
edu:deploy: at addChunk (node:internal/streams/readable:324:12)
edu:deploy: npm ERR! Lifecycle script `build` failed with error:
edu:deploy: npm ERR! Error: command failed
edu:deploy: npm ERR! in workspace: edu@0.0.1
edu:deploy: npm ERR! at location: /Users/mark/work/turbo_platforms/apps/edu
edu:deploy:
edu:deploy: ✘ [ERROR] Command failed with exit code 1: npm run build
It seems like telefunc depends on node's path, which is not supported by the workers environment.
Is there anything I can do to fix this?
Cloudflare Workers does include a node shim, but you have to opt into it.
Also FYI https://github.com/brillout/telefunc/tree/main/examples/cloudflare-workers.
In principle, the dependency to 'path'
could be removed. Up for a PR?
I have tried adding compatibility_flags = ["nodejs_compat"]
into wrangler.toml (https://developers.cloudflare.com/workers/configuration/compatibility-dates/#nodejs-compatibility-flag).
However, the build still fails with the same error.
It seems like it only works when you have imports with a node:
prefix:
// Do this:
import { Buffer } from 'node:buffer';
// Do not do this:
import { Buffer } from 'buffer';
(From https://developers.cloudflare.com/workers/runtime-apis/nodejs)
And telefuc imports 'path' directly
telefunc/telefunc/utils/getOutDirs.ts
Line 7 in 8f537de
I have checked the cloudflare workers example, but I have no idea why it works but mine does not
Even after manually adding node:path
and adding node:*
to esbuild's external, I am still stuck on an error:
edu:preview: Error: Dynamic require of "node:path" is not supported
edu:preview: at worker.js:6:9
edu:preview: at node_modules/telefunc/dist/cjs/node/server/serverConfig.js (worker.js:48001:18)
edu:preview: at __require22 (worker.js:75:50)
edu:preview: at node_modules/telefunc/dist/cjs/node/vite/plugins/importBuild/loadBuild.js (worker.js:48204:26)
edu:preview: at __require22 (worker.js:75:50)
edu:preview: at node_modules/telefunc/dist/cjs/node/vite/loadTelefuncFilesWithVite.js (worker.js:48433:23)
edu:preview: at __require22 (worker.js:75:50)
edu:preview: at node_modules/telefunc/dist/cjs/node/server/runTelefunc/loadTelefuncFiles.js (worker.js:48564:39)
edu:preview: at __require22 (worker.js:75:50)
edu:preview: at node_modules/telefunc/dist/cjs/node/server/runTelefunc.js (worker.js:49630:31) {
edu:preview: stack: Error: Dynamic require of "node:path" is not suppo…s/node/server/runTelefunc.js (worker.js:49630:31),
edu:preview: message: Dynamic require of "node:path" is not supported
Seems like the only solution is to migrate away from 'path'. Was really hoping to get telefunc to work with sveltekit and workers, what a bummer
Is there any way we can reopen this issue @brillout ?
Since there is an official working example, I'm inclined to think that it's a user land problem not a Telefunc one.
If you can create a minimal reproduction starting off https://github.com/brillout/telefunc/tree/main/examples/cloudflare-workers, I'll have a look at it.
Also could not get this to work - same error - Could not resolve "path" :(
@impactvelocity Minimal reproduction welcome.