how to handle dependencies using node built-ins
Hey! I'm new to Cloudflare, and trying to convert my node server to work in Cloudflare workers instead. While my core code seems to work fine, I'm having issues with various dependencies that rely on node built-ins. I'm a bit confused as to how to handle these - is there a way to polyfill / compile with webpack or something more thorough than just the node_compat flag? Or for these packages require node:fs, node:util, node:stream should I just avoid entirely ? Will send the error message below
5 Replies
yarn wrangler:dev
โ
๏ธ wrangler 3.15.0
-------------------
โฒ [WARNING] Processing src/wrangler.toml configuration:
- ๐ถ Ignored: "type":
Most common features now work out of the box with wrangler, including modules, jsx,
typescript, etc. If you need anything more, use a custom build.
โฒ [WARNING] Enabling Node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details.
โ [ERROR] No matching export in "node-modules-polyfills:fs" for import "statSync"
../node_modules/fetch-blob/from.js:1:9:
1 โ import { statSync, createReadStream, promises as fs } from 'node:fs'
โต ~~
โ [ERROR] No matching export in "node-modules-polyfills:fs" for import "createReadStream"
../node_modules/fetch-blob/from.js:1:19:
1 โ import { statSync, createReadStream, promises as fs } from 'node:fs'
โต ~~~~
โ [ERROR] No matching export in "node-modules-polyfills:fs" for import "promises"
../node_modules/fetch-blob/from.js:1:37:
1 โ import { statSync, createReadStream, promises as fs } from 'node:fs'
โต ~~
โ [ERROR] No matching export in "node-modules-polyfills:util" for import "types"
../node_modules/node-fetch/src/body.js:9:8:
9 โ import {types, deprecate, promisify} from 'node:util';
โต ~
โ [ERROR] No matching export in "node-modules-polyfills:util" for import "promisify"
../node_modules/node-fetch/src/body.js:9:26:
9 โ import {types, deprecate, promisify} from 'node:util';
โต ~~~
โ [ERROR] No matching export in "node-modules-polyfills:util" for import "types"
../node_modules/node-fetch/src/headers.js:7:8:
7 โ import {types} from 'node:util';
โต ~~~
GitHub
GitHub - ionic-team/rollup-plugin-node-polyfills
Contribute to ionic-team/rollup-plugin-node-polyfills development by creating an account on GitHub.
โ [ERROR] No matching export in "node-modules-polyfills:stream" for import "pipeline"
../node_modules/node-fetch/src/index.js:12:29:
12 โ import Stream, {PassThrough, pipeline as pump} from 'node:stream';
โต ~~
โ [ERROR] No matching export in "node-modules-polyfills:net" for import "isIP"
../node_modules/node-fetch/src/utils/referrer.js:1:8:
1 โ import {isIP} from 'node:net';
โต
โ [ERROR] No matching export in "node-modules-polyfills:url" for import "fileURLToPath"
../node_modules/trpc-panel/lib/index.mjs:2:9:
2 โ import { fileURLToPath } from 'node:url';
โต ~~~
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ [b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โ [ERROR] Failed to build
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose
GitHub
Build software better, together
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 330 million projects.
Some of these node modules, like
fs
, just plain won't work on Workers. And node-fetch
isn't needed, since Workers already has fetch. I would probably use a differnet package if possible.
Also, it looks like you are using an older config, back when wrangler
still used Webpack for bundlingThanks a ton for the response! Is there a wrangler doctor type of command that helps detect what packages are causing these errors? Or is it more of a trial and error thing just running wrangler + yarn why until I can get it working?
Appreciate your help ๐
No command as far as I'm aware, other than the warnings that you saw. I would recommend going for packages that target browser compatibility, or otherwise check whether they stick to the following Node APIs, which are supported by Workers.
Node.js compatibility ยท Cloudflare Workers docs
Most Workers import one or more packages of JavaScript or TypeScript code from npm as dependencies in package.json. Many of these packages rely on โฆ