Announcing GrowthBook on JSR

Ryan Feigenbaum - Nov 4 - - Dev Community

Announcing GrowthBook on JSR

GrowthBook is committed to supporting modern platforms, bringing advanced feature flagging and experimentation to where you are. We’re excited to announce the availability of our JavaScript SDK on JSR, the modern open-source JavaScript registry. This integration empowers JavaScript and Typescript developers with a seamless experience for implementing and managing feature flags in their applications.

JSR simplifies the process of publishing and importing JavaScript modules, offering robust features like TypeScript support, auto-generated documentation, and enhanced security through provenance attestation. This collaboration brings these benefits to GrowthBook users, streamlining the integration and utilization of feature flagging in their development workflows.

Using the GrowthBook JS SDK via JSR offers an excellent developer experience, with first-class TypeScript support, auto-generated documentation accessible in your code editor, and more.

How to Install GrowthBook from JSR

Get started with GrowthBook using the deno add command:

deno add jsr:@growthbook/growthbook
Enter fullscreen mode Exit fullscreen mode

Or using npm:

npx jsr add @growthbook/growthbook
Enter fullscreen mode Exit fullscreen mode

The above commands will generate a deno.json file, listing all project dependencies.

{
  "imports": {
    "@growthbook/growthbook": "jsr:@growthbook/growthbook@0.1.2"
  }
}

Enter fullscreen mode Exit fullscreen mode

deno.json

Use GrowthBook with Express

Let’s use GrowthBook with an Express server. In our main.ts file, we can write:

import express from "express";
import { GrowthBook } from "@growthbook/growthbook";

const app = express();

// Example using Express
app.use(function (req, res, next) {
  // Create a GrowthBook instance and store in the request
  req.growthbook = new GrowthBook({
    apiHost: "<https://cdn.growthbook.io>",
    clientKey: "sdk-qtIKLlwNVKxdMIA5",
  });

  // TODO: Add user targeting attributes from cookies, headers, etc.
  req.growthbook.setAttributes({
    id: req.user?.id,
  });

  // Clean up at the end of the request
  res.on("close", () => req.growthbook.destroy());

  // Wait for features to load (will be cached in-memory for future requests)
  req.growthbook.init({ timeout: 1000 }).then(() => next());
});

app.get("/", (req, res) => {
  const gb = req.growthbook;

  // Boolean on/off flag
  if (gb.isOn("my-boolean-feature")) {
    res.send("Hello, boolean-feature!");
  }

  // String/Number/JSON flag
  const value = gb.getFeatureValue("my-string-feature", "fallback");

  res.send(`Hello, ${value}!`);
});

console.log("Listening on port 8000");
app.listen(8000);

Enter fullscreen mode Exit fullscreen mode

Finally, you can run the following command to execute:

deno -A main.ts
Enter fullscreen mode Exit fullscreen mode

Depending on how you’ve set up your feature flags in GrowthBook (sign up for free), the response will be different:

Announcing GrowthBook on JSR

Check out our official docs to learn more about feature flags, creating and running experiments, and analyzing experiments.

What’s next?

With GrowthBook's JS SDK now on JSR, it’s even easier to bring the power of feature flags and A/B testing to any JavaScript environment.

. . . . . . . . . . . . . . .