LaunchDarkly Node.js Client-Side SDK

3.3.1 · active · verified Tue Apr 21

This package provides the LaunchDarkly Client-Side SDK for Node.js environments, specifically designed for applications deployed to an end-user, such as desktop apps or smart devices, rather than multi-user server-side systems. It enables developers to integrate feature flagging capabilities into their Node.js client applications, allowing for controlled feature rollouts, A/B testing, and dynamic configuration. The current stable version is 3.3.1. The SDK maintains an active release cadence, with updates typically every few months, addressing bug fixes, performance improvements, and new features like client-side prerequisite events and enhanced error handling. A key differentiator is its explicit focus on single-user contexts, contrasting with the separate server-side SDK for multi-user applications, and its evolution to support custom contexts over the older 'users' concept in version 3.0.0.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the LaunchDarkly client with a context, wait for initialization, evaluate a boolean and string feature flag, and track a custom event.

import { initialize, LDContext, LDClient } from 'launchdarkly-node-client-sdk';

const clientSideId = process.env.LAUNCHDARKLY_CLIENT_SIDE_ID ?? 'YOUR_CLIENT_SIDE_ID_HERE';

if (clientSideId === 'YOUR_CLIENT_SIDE_ID_HERE') {
  console.warn('Please set the LAUNCHDARKLY_CLIENT_SIDE_ID environment variable or replace the placeholder.');
}

async function runFeatureFlagExample() {
  const context: LDContext = {
    kind: 'user',
    key: 'example-user-key',
    name: 'Example User',
    // You can add custom attributes here, e.g.:
    // country: 'us',
    // anonymous: true,
  };

  console.log('Initializing LaunchDarkly client...');
  const client: LDClient = initialize(clientSideId, context);

  try {
    // Wait for the client to connect to LaunchDarkly. A timeout is highly recommended.
    // e.g., await client.waitForInitialization(5000);
    await client.waitForInitialization();
    console.log('LaunchDarkly client initialized.');

    // Evaluate a feature flag
    const showNewFeature = client.variation('new-feature', false); // 'new-feature' is flag key, false is default value
    console.log(`Feature flag 'new-feature' is ${showNewFeature ? 'ON' : 'OFF'}.`);

    const welcomeMessage = client.variation('welcome-message', 'Hello, user!');
    console.log(`Welcome message: "${welcomeMessage}"`);

    // You can also track custom events
    client.track('button-click', context, { buttonId: 'primary-cta' });
    console.log('Custom event "button-click" tracked.');

  } catch (error) {
    console.error('Failed to initialize LaunchDarkly client or evaluate flags:', error);
  } finally {
    // Always close the client when done to ensure all events are sent and connections are closed
    console.log('Closing LaunchDarkly client...');
    client.close();
    console.log('Client closed.');
  }
}

runFeatureFlagExample();

view raw JSON →