https://www.growthbook.io/ logo
#contributing
Title
# contributing
r

refined-city-47392

06/14/2022, 5:58 AM
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:
Copy code
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:
Copy code
(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
Copy code
{
  "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!
f

fresh-football-47124

06/14/2022, 7:35 AM
Hi Jacob - can you share the package.json?
r

refined-city-47392

06/14/2022, 7:38 AM
Absolutely
Copy code
{
  "name": "node-typescript-boilerplate",
  "version": "0.0.0",
  "description": "Minimalistic boilerplate to quick-start Node.js development in TypeScript.",
  "type": "module",
  "devDependencies": {
    "@types/jest": "~27.5",
    "@types/node": "~16",
    "@typescript-eslint/eslint-plugin": "~5.26",
    "@typescript-eslint/parser": "~5.26",
    "eslint": "~8.16",
    "eslint-config-prettier": "~8.5",
    "eslint-plugin-jest": "~26.2",
    "jest": "~28.1",
    "prettier": "~2.6",
    "rimraf": "~3.0",
    "ts-jest": "~28.0",
    "tsutils": "~3.21",
    "typescript": "~4.7"
  },
  "scripts": {
    "start": "node build/src/main.js",
    "clean": "rimraf coverage build tmp",
    "prebuild": "npm run lint",
    "build": "tsc -p tsconfig.json",
    "build:watch": "tsc -w -p tsconfig.json",
    "build:release": "npm run clean && tsc -p tsconfig.release.json",
    "lint": "eslint . --ext .ts --ext .mts",
    "test": "jest --coverage",
    "prettier": "prettier --config .prettierrc --write .",
    "test:watch": "jest --watch"
  },
  "author": "Jakub Synowiec <jsynowiec@users.noreply.github.com>",
  "license": "Apache-2.0",
  "dependencies": {
    "@growthbook/growthbook": "^0.18.0",
    "@sentry/node": "^7.1.1",
    "@sentry/tracing": "^7.1.1",
    "@types/amqplib": "^0.8.2",
    "@types/json-diff": "^0.7.0",
    "@types/pg": "^8.6.5",
    "amqplib": "^0.10.0",
    "envalid": "^7.3.1",
    "json-diff": "^0.9.0",
    "pg": "^8.7.3",
    "tslib": "~2.4",
    "uuid": "^8.3.2"
  },
  "volta": {
    "node": "18.3.0"
  }
}
f

fresh-football-47124

06/14/2022, 7:42 AM
how did you install growthbook? npm install?
r

refined-city-47392

06/14/2022, 7:42 AM
correct
f

fresh-football-47124

06/14/2022, 7:47 AM
hrmm, still investigating ...
r

refined-city-47392

06/14/2022, 7:48 AM
Thank you! I’m sure it’s something I’ve screwed up, but I’m failing to figure out what it is
f

fresh-football-47124

06/14/2022, 9:00 AM
okay, stumped me - If you're around in a few hours @future-teacher-7046 can help you more - or you could open a GitHub issue with this as well.
👍 1
f

future-teacher-7046

06/14/2022, 11:25 AM
It might be something with Node 18 or Typescript 4.7, which I don't think we've tested the library with. Are you able to import from the esm dist directory?
@growthbook/growthbook/dist/esm
r

refined-city-47392

06/14/2022, 11:28 AM
Copy code
node:internal/errors:466
  ErrorCaptureStackTrace(err);
  ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/esm' is not defined by "exports" in /Users/xxx/node_modules/@growthbook/growthbook/package.json imported from /Users/xxx/build/src/main.js
  at new NodeError (node:internal/errors:377:5)
  at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)
  at packageExportsResolve (node:internal/modules/esm/resolve:719:3)
  at packageResolve (node:internal/modules/esm/resolve:893:14)
  at moduleResolve (node:internal/modules/esm/resolve:959:20)
  at defaultResolve (node:internal/modules/esm/resolve:1174:11)
  at ESMLoader.resolve (node:internal/modules/esm/loader:605:30)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:318:18)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
  at link (node:internal/modules/esm/module_job:78:36) {
 code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
f

future-teacher-7046

06/14/2022, 11:36 AM
Hmm. One more thing to try. If you can edit
node_modules/@growthbook/growthbook/package.json
and add
"type": "module",
that might work. I know Typescript 4.7 changed a bunch of stuff when it comes to esm with node. I'll also check to see how
@sentry/node
is doing things since that seems to be working for you.
Only difference I see is that we specify an
exports
block in our
package.json
and sentry does not.
r

refined-city-47392

06/14/2022, 11:50 AM
When adding “type” : “module” I still got errors
Copy code
node:internal/errors:466
  ErrorCaptureStackTrace(err);
  ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/xxx/node_modules/@growthbook/growthbook/dist/esm/GrowthBook' imported from /Users/xxx/node_modules/@growthbook/growthbook/dist/esm/index.js
However, when I started checking that code, I saw that
.js
was missing from the import path in the following places:
node_modules/@growthbook/growthbook/dist/esm/index.js
-> missing in
export { GrowthBook } from "./GrowthBook";
node_modules/@growthbook/growthbook/dist/esm/GrowthBook.js
-> missing in
util
and
mongrule
import By adding .js to those three places (combined with adding
"type" : "module"
to package.json) it started up just fine!
f

future-teacher-7046

06/14/2022, 12:01 PM
Thanks for doing that debugging! I'll see if I can replicate the bug and make sure those fixes won't break older node/Typescript versions.
r

refined-city-47392

06/14/2022, 12:03 PM
np! I’m just excited to start using growthbook
Is there any way to get hold of a generated SKD with fixes? I forked the repo thinking I could add the fixes and just point to the repo in my package.json (until the fix in place in the official repo). But then I remembered it needs to be built and made available.
f

future-teacher-7046

06/17/2022, 11:46 AM
Are you able to make a PR with your fixes?
a

ambitious-crowd-69774

07/22/2022, 4:06 AM
@refined-city-47392 we're you able to get this fixed ? Experiencing this issue right now with a brand new sveltekit project…
r

refined-city-47392

07/22/2022, 2:40 PM
No, didn’t have time to fix it properly. I’ve been hoping someone else would pick it up…
a

ambitious-crowd-69774

07/22/2022, 2:57 PM
I tried this morning and I think it has something to do with the typescript compilation
r

refined-city-47392

07/22/2022, 3:24 PM
Yeah, the problem is that “type” : “module” is missing from package.json and that there are three places where “.js” is missing from import paths. (https://growthbookusers.slack.com/archives/C02F2P3LZ35/p1655207401589009?thread_ts=1655186325.646539&amp;cid=C02F2P3LZ35)
d

dazzling-address-78247

10/19/2022, 2:15 PM
Hi @future-teacher-7046, I'm facing same problem. Did you guys found a solution for it?
f

fresh-football-47124

10/19/2022, 4:34 PM
@dazzling-address-78247 what node version are you using?
d

dazzling-address-78247

10/19/2022, 4:34 PM
Copy code
$ node -v
v16.14.0
I did the type:module, and added the ".js" on imports, and it started working. However, I'm not sure how I can deploy these changes in Container since it's going to run it's own npm install 😕
r

refined-city-47392

10/19/2022, 4:47 PM
My ugly temporary solution was the copy the entire dir from the node_modules into my repo and apply the fixes there and check in the code. Then I just import the lib using the local path. It’s not pretty, but it works fine until the npm lib has been updated.
d

dazzling-address-78247

10/19/2022, 5:05 PM
Yeah, that's what I was going to do. I'll miss on the updates, but i'd miss them anyway sticking to the specific version 🙂 Thank you sir! Are there any plans to fix it in some distant future, or is it fixable at all?
a

adorable-minister-56906

02/15/2024, 9:27 AM
I'm not sure if they have fixed it yet or not. But as I try, I still get an error when importing it into the module setup. I tried integrating Growthbook using Astro. Adding 'type="module"' and changing the import also works. However, if you don't want to modify the node_module, you can import it from CJS using the following code.
Copy code
import { createRequire } from "module";
const require = createRequire(import.meta.url);

const { GrowthBook } = require("@growthbook/growthbook");
318 Views