growthbook / growthbook

Open Source Feature Flagging and A/B Testing Platform

Home Page:https://www.growthbook.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] Growthbook import error on Astro SSR

rbrishabh opened this issue · comments

Summary

When importing {GrowthBook} from '@growthbook/growthbook' or ('@growthbook/react') inside a Astro component OR inside a React Component which is part of Astro, there is the error:

"Named export 'GrowthBook' not found. The requested module '@growthbook/growthbook' is a CommonJS module, which may not support all module.exports as named exports.\nCommonJS modules can always be imported via the default export, for example using:\n\nimport pkg from '@growthbook/growthbook';\nconst { GrowthBook } = pkg;\n"

Expected Behavior

It imports without any issues.

Current Behavior

To make it work properly, I need to manually import from node modules like this:
import {GrowthBook} from "./../../node_modules/@growthbook/growthbook/dist/esm";

My package.json is:
"type": "module",

Growthbook version: "@growthbook/growthbook": "^0.32.0",
Astro version: ^3.5.4
Node Version: v18.16.0

Let me know if you need any more details from my side! Thank you :)
More about Astro: https://docs.astro.build/en/getting-started/

Thank you!

@Auz please prioritze this with our custom dev hours.

Maybe related, see the fix: getsentry/sentry-javascript#10042

Hello, any updates on this? :)

@rbrishabh What @RiZKiT pointed to does seem to work for this. You can put the following code in your astro.config.mjs:

import { defineConfig } from "astro/config";

export default defineConfig({
  vite: {
    ssr: {
      noExternal: ["@growthbook/growthbook"],
    },
  },
});