Hi guys! I’m having some package/module issues using the SDK in node (for a backend service). I’ve setup a typescript project, installed a bunch of dependencies and everything works (including building the app with tic) except loading the growthbook sdk at runtime. I’m getting the following error:
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;
My IDE (Vscode) has no problem loading the sdk as an ES6 module. No other libs are causing any issues loading them with import. Just to humor node, I tried the suggestion and got the following error:
(node:20837) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/Users/xxxx/node_modules/@growthbook/growthbook/dist/esm/index.js:1
export { GrowthBook } from "./GrowthBook";
^^^^^^
SyntaxError: Unexpected token 'export'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Module._load (node:internal/modules/cjs/loader:827:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
My tsconfig.json is as follows
{
"compilerOptions": {
"target": "ES2022",
"module": "es6",
"lib": ["ES2022", "dom"],
"moduleResolution": "node",
"rootDir": ".",
"outDir": "build",
"allowSyntheticDefaultImports": true,
"importHelpers": true,
"alwaysStrict": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"include": ["src/**/*", "__tests__/**/*"]
}
Versions:
• `node`: 18.3.0
• `@growthbook/growthbook`: 0.18.0
• `tsc`: 4.7.2
Cmd to build is
tsc -p tsconfig.json
Cmd to run
node build/src/main.js
I spent a good deal of time yesterday trying to figure out the issue but to no avail. Any ideas/suggestions are much appreciated!