https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
m

microscopic-refrigerator-66345

02/16/2023, 9:46 PM
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.
Copy code
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)
Copy code
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
Copy code
(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
Copy code
"type": "module",
And run again the code. This time the error message was this
Copy code
./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
Copy code
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:
Copy code
(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?
s

swift-helmet-3648

02/17/2023, 2:45 AM
Hi @microscopic-refrigerator-66345 For vanilla Node.js, you should use the CommonJS style of importing instead (e.g.
const GrowthBook = require('@growthbook/growthbook').GrowthBook
). (Our Node.js section in the docs hints at this.) We don't support ESM imports currently. If I may ask, do you know what sort of project you intend to use GrowthBook for? Typically our SDK is used with a web server such as Express. You might have a better time trying out GrowthBook by setting up a web server in Node.js first, and then installing GrowthBook onto it.
Copy code
const GrowthBook = require("@growthbook/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
gb.loadFeatures({ autoRefresh: true }).then(() => {
  const color = gb.getFeatureValue("button-color");
  console.log("color", color);
});
m

microscopic-refrigerator-66345

02/17/2023, 4:07 AM
Thank you, it worked! I’m planning to use GrowthBook in a particular project for change the behavior for my node application;
25 Views