Hi everyone, I need some help to initiate the growthbook project with vanilla nodejs. So, I iniciate...
f

Fernando Giaretta

over 2 years ago
Hi everyone, I need some help to initiate the growthbook project with vanilla nodejs. So, I iniciated a NodeJS (v18.12.1) empty project to test the GrowthBook tool.
mkdir test
cd test
npm init -y
npm install --save @growthbook/growthbook
touch index.js
inside
index.js
file, I paste the Step 1 from the guide (https://docs.growthbook.io/lib/js)
import { GrowthBook } from "@growthbook/growthbook";

// Create a GrowthBook instance
const gb = new GrowthBook({
  apiHost: "<https://cdn.growthbook.io>",
  clientKey: "sdk-key",
  // Enable easier debugging of feature flags during development
  enableDevMode: true,
});

// Wait for features to be available
await gb.loadFeatures({ autoRefresh: true });

const color = gb.getFeatureValue("button-color");
When I executed
node index.js
, the terminal yield
(node:38068) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
./tmp/test/index.js:1
import { GrowthBook } from "@growthbook/growthbook";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.12.1
Then I add this line to
package.json
"type": "module",
And run again the code. This time the error message was this
./tmp/test/index.js:1
import { GrowthBook } from "@growthbook/growthbook";
         ^^^^^^^^^^
SyntaxError: Named export 'GrowthBook' not found. The requested module '@growthbook/growthbook' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@growthbook/growthbook';
const { GrowthBook } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.12.1
And then, I change the
index.js
file to this
import pkg from '@growthbook/growthbook';
const { GrowthBook } = pkg;

// Create a GrowthBook instance
const gb = new GrowthBook({
  apiHost: "<https://cdn.growthbook.io>",
  clientKey: "sdk-key",
  // Enable easier debugging of feature flags during development
  enableDevMode: true,
});

// Wait for features to be available
await gb.loadFeatures({ autoRefresh: true });

const color = gb.getFeatureValue("button-color");
And run the nodejs file again. This time I received this error message:
(node:38721) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
./tmp/test/node_modules/@growthbook/growthbook/dist/esm/index.js:1
export { setPolyfills, clearCache, configureCache } from "./feature-repository";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)

Node.js v18.12.1
Can get any help?
Hi all, I'm working on getting GrowthBook integrated into a React Js application to get rid of our ...
j

Josh Stafford

over 2 years ago
Hi all, I'm working on getting GrowthBook integrated into a React Js application to get rid of our current solution. I've got the initialisation working in the
App.js
file but I have a redux reducer where I'm attempting to setAttributes on a user any time an update happens to the user within our application. I have a few questions... • Is this possible? The reason I'm doing this is we have some features which are enabled for certain groups of people. ie. internal features only enabled for users with certain emails/company information on their client data. • When i set the attributes they dont get updated on the attributes screen here https://app.growthbook.io/attributes is there something I'm doing wrong? • The Chrome plugin for Growthbook doesn't seem to be working, do I need any setup in order to get this going? • Is there somewhere that I can have a development SDK key and a production SDK key, reason being for testing tickets in a development environment with flags and then passing them through or duplicating them into prod. • I'd love to move to a paid plan but can I trial this first? Appreciate the help in advance! Here's the code I'm using in the reducer in order to try and set the client attributes.
reducers: {
    setClientData: (state, action) => {
      console.log("Updating growthbook data with", action.payload)
      growthbook.setAttributes({
        "id": fire.auth().currentUser.uid,
        ...action.payload,
      });
      state.data = action.payload;
    },
  } ...
Hi guys, been using the growth book at my org for the last few months and am quite amazed by its eas...
a

Abhushan Adhikari

about 3 years ago
Hi guys, been using the growth book at my org for the last few months and am quite amazed by its ease of use from a developer's perspective. I just wanted to know if there should also be sections in docs specified on how to use it in an SSR context, especially with frameworks like Next.js and Remix. ? The reason is, that I think it might help developers take the right approach based on their framework type. Considering the docs show the usage strictly on client which is not very suitable due to flickering/CLS/ blocking UI and that can be avoided easily if you have SSR react in some way... Maybe we can add a section in the context of the SSR application where essentially we do the same but on the server data fetcher logic and still pass the features list to the provider. One possible way in Next.js specific could be something along the lines of
export const getServerSideProps= async ()=>{
 const gbFeatures= await fetchGrowthbookFeatures();

 return {
  gbFeatures
 }

}
On my
_app.tsx
I did something or of sort
const growthbook = useMemo(()=>{
 return new Growthbook({
    features: pageProps?.gbFeatures?.features,
     attributes: ....// we can set them here as well
 })
},[pageProps])
useEffect(()=>{
  if( pageProps?.gbFeatures) return ;  // since SSR handled it

fetch('apiendpoint').then()//// the usual client approach as per doc
 
},[growthbook, pageProps?.gbFeatures?])
<GrowthbookProvider growthbook={growthbook}>{children}</GrowthbookProvider>
Any thoughts on this? Happy to contribute and raise a PR..
Hi all, I'm trying to run growthbook self-hosted on AWS, and I've managed to get the `growthbook/gr...
s

Spencer Kohan

9 months ago
Hi all, I'm trying to run growthbook self-hosted on AWS, and I've managed to get the
growthbook/growthbook:latest
container up and running and connected to a
DocumentsDB
cluster, but when I actually try to access the webapp, I get this error: > Error connecting to the GrowthBook API ... No authorization token was found From what I can tell it seems to be maybe related to this issue: https://linen.growthbook.io/t/34006/i-have-another-question-whilst-we-are-exploring-the-product- What I don't understand is, it seems to be the request to /auth/refresh which is returning 401, which doesn't make sense because I understood the token is expected not to exist, and should be generated on the first refresh Here's what I'm seeing from the container logs (note, I have the api mapped to host/api-route/ via an application load balancer):
{"level":50,"time":1727955520197,"pid":75,"hostname":"ip-10-0-165-156.eu-west-1.compute.internal","req":{"id":5,"method":"POST","url":"/api-route/auth/refresh","query":{},"params":{},"headers":{"x-forwarded-for":"94.134.28.56","x-forwarded-proto":"http","x-forwarded-port":"80","host":<host>,"x-amzn-trace-id":"Root=1-66fe8240-305abbc60e0d26dc5bbe8a0a","content-length":"0","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36","dnt":"1","origin":<host>,"referer":<host>},"remoteAddress":"::ffff:10.0.116.205","remotePort":62760},"msg":"No authorization token was found"}
Is this a known issue / is there anything I can do to get around it?