LaunchDarkly Node.js Client-Side SDK
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
-
TypeError: LaunchDarkly.initialize is not a function
cause Incorrect import statement for ES Modules or CommonJS.fixIf using ES Modules (TypeScript/modern Node.js), use `import { initialize } from 'launchdarkly-node-client-sdk';`. If using CommonJS, use `const { initialize } = require('launchdarkly-node-client-sdk');` or `const LaunchDarkly = require('launchdarkly-node-client-sdk');` and then `LaunchDarkly.initialize()`. -
LaunchDarkly SDK is not initialized. Flag 'some-flag' will return default value.
cause A `variation()` call was made before the SDK had successfully initialized and connected to LaunchDarkly.fixEnsure `await client.waitForInitialization()` (with an optional timeout) is called and resolved before attempting to evaluate feature flags. This guarantees the SDK has fetched flag configurations. -
LaunchDarkly: The SDK key you provided does not appear to be a client-side SDK key.
cause A server-side SDK key was used instead of a client-side SDK key during initialization.fixVerify that you are using a *client-side* SDK key obtained from your LaunchDarkly project settings. Client-side keys are designed to be embedded in client applications and usually start with `mob-` or `client-`. Never use a server-side SDK key (which is a secret) in client-side code.
Warnings
- breaking Version 3.0.0 introduced a breaking change by replacing the `user` concept with `context` (or `LDContext`). All flag evaluations and SDK interactions now require an `LDContext` object instead of a `user` object. Code written for v2.x will fail if still passing `user` objects.
- gotcha This SDK (`launchdarkly-node-client-sdk`) is designed exclusively for client-side Node.js applications (e.g., desktop apps or IoT devices) where a single client instance serves a single user context. It is explicitly *not* intended for multi-user server-side applications. Using it in a server environment will lead to incorrect flag evaluations, performance issues, and potential data privacy concerns.
- gotcha Prior to v3.2.0, `waitForInitialization` would wait indefinitely if no timeout was specified. Since v3.2.0, you can now provide an optional timeout (e.g., `client.waitForInitialization(5000)`) which will reject the promise if initialization does not complete within the specified time. If no timeout is specified, it will still wait indefinitely.
- gotcha Version 3.0.2 updated `LDContext` to allow the `key` property to be optional, specifically for anonymous contexts where a key can be generated. However, it is generally recommended to provide a specific `key` for better traceability, targeting, and consistency across sessions, unless an anonymous context with a generated key is explicitly desired.
Install
-
npm install launchdarkly-node-client-sdk -
yarn add launchdarkly-node-client-sdk -
pnpm add launchdarkly-node-client-sdk
Imports
- initialize
const initialize = require('launchdarkly-node-client-sdk').initialize;import { initialize } from 'launchdarkly-node-client-sdk'; - LDContext
import { LDContext } from 'launchdarkly-node-client-sdk'; - LDClient
import { LDClient } from 'launchdarkly-node-client-sdk';
Quickstart
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();