Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: compatibility with Cloudflare Workers #61

Open
emmanuelsw opened this issue Mar 3, 2025 · 3 comments
Open

[feat]: compatibility with Cloudflare Workers #61

emmanuelsw opened this issue Mar 3, 2025 · 3 comments

Comments

@emmanuelsw
Copy link

A clear and concise description of what the feature is

Hello! I'm trying to use this library in an Astro project that I deploy on Cloudflare Pages, but I haven't been able to make it work. I followed Cloudflare's documentation to add polyfills for some Node APIs that were generating the error, but the Worker still throws this error:

[ERROR] Error: No such module \"node:tty\".\n imported from \"chunks/strapi-client_R71o50vs.mjs

Is there any plans to support the Cloudflare Workers runtime in the future?

Why should this feature be included?

It would increase the number of users who could use this library, in different contexts.

Please provide an example for how this would work

No response

@nailchu
Copy link

nailchu commented Mar 5, 2025

It also occured on me.

I created a new Vue or React frontend project, added @strapi/client package, run build and It said I need 3 modules: os, util and tty. They are modules of Nodejs and not exist in browsers.
Build output :

[plugin vite:resolve] Module "tty" has been externalized for browser compatibility, imported by "/root/testtest/node_modules/.pnpm/@[email protected]/node_modules/@strapi/client/dist/bundle.mjs". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin vite:resolve] Module "util" has been externalized for browser compatibility, imported by "/root/testtest/node_modules/.pnpm/@[email protected]/node_modules/@strapi/client/dist/bundle.mjs". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin vite:resolve] Module "os" has been externalized for browser compatibility, imported by "/root/testtest/node_modules/.pnpm/@[email protected]/node_modules/@strapi/client/dist/bundle.mjs". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.

Browser console output:
Image

I went to some LLM chatbots and this solution work:

  • add 3 packages: tty-browserify,os-browserify,util
  • add package vite-plugin-node-polyfills (I am using Vite)
  • add configs below to vite.config.js
import { nodePolyfills } from 'vite-plugin-node-polyfills'
export default defineConfig({
  plugins: [
    nodePolyfills({
      include: ['os', 'util', 'tty'],
      globals: { Buffer: true}
    }),
  ],
  resolve: {
    alias: {
      'os': 'os-browserify/browser',
      'util': 'util',
      'tty': 'tty-browerify'
    },
  },
  optimizeDeps: {
    exclude: ['os-browserify', 'tty-browserify']
  }
})

Now there are no errors and everything work.

@nailchu
Copy link

nailchu commented Mar 10, 2025

Hey, @emmanuelsw
I did some futher study and this #62 might help you.
You can add "browser": { "@strapi/client": "./node_modules/@strapi/client/dist/new_esm_build.js"} to package.json if your build tool is not Vite.

@Convly
Copy link
Member

Convly commented Mar 11, 2025

Hey there, thanks for reporting this issue. We're going to work on both this and #62 as soon as we can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants