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
Bierman
Biermanโ€ข9mo ago
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.
Bierman
Biermanโ€ข9mo ago
โœ˜ [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.
Hello, Iโ€™m Allie!
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 bundling
Bierman
Biermanโ€ข9mo ago
Thanks 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 ๐Ÿ™‚
Hello, Iโ€™m Allie!
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 โ€ฆ