LaunchDarkly JavaScript Client SDK (Legacy)
The `launchdarkly-js-client-sdk` package provides client-side feature flag management for JavaScript applications running in web browsers. It allows developers to control features remotely without redeploying code, enabling practices like A/B testing, gradual rollouts, and kill switches. This specific package is currently at version 3.9.1 but is officially deprecated. All future development and releases are now happening under the `@launchdarkly/js-client-sdk` monorepo package. It leverages the 'Client-side ID' for authentication and requires flags to be explicitly made available to client-side SDKs. Release cadence was previously frequent for this package, but it is now in maintenance mode, with new features directed to the new package. Its primary differentiator is robust feature flag management with detailed event tracking and a strong focus on enterprise features, often used in conjunction with React through the `react-client-sdk` addon.
Common errors
-
LaunchDarkly client did not initialize. Please check your SDK key and network connection.
cause Incorrect Client-side ID, network connectivity issues, or firewall blocking access to LaunchDarkly servers.fixDouble-check the provided Client-side ID against your LaunchDarkly project settings. Ensure your client device has active internet access and is not being blocked by a firewall or proxy from reaching LaunchDarkly endpoints. -
ReferenceError: Promise is not defined
cause The browser environment does not natively support the `Promise` object, and no polyfill has been included.fixAdd a `Promise` polyfill (e.g., from `core-js` or `es6-promise`) to your application bundle to ensure compatibility with older browsers. -
TypeError: ldClient.variation is not a function
cause Attempting to call `variation` on an `ldClient` instance that has not yet completed initialization or is undefined due to initialization failure.fixEnsure you call `ldClient.variation` only after the `ready` event fires or `waitForInitialization()` resolves, indicating the client is ready to provide flag values. Handle potential initialization failures gracefully.
Warnings
- breaking This `launchdarkly-js-client-sdk` package is officially deprecated. All future development, bug fixes, and new features will be released under the new monorepo package `@launchdarkly/js-client-sdk`. Users are strongly encouraged to migrate.
- gotcha The SDK requires a 'Client-side ID' from your LaunchDarkly account settings, not the 'SDK key' or 'Mobile key'. Using the incorrect key will prevent the client from initializing and fetching flags.
- gotcha For any feature flag to be accessible in the client-side SDK, the 'Make this flag available to client-side SDKs' checkbox must be enabled on the flag's settings page in the LaunchDarkly dashboard.
- gotcha Older browser environments may lack native support for features like `Promise`, `EventSource`, or `document.querySelectorAll()`, which are used by the SDK. This can lead to runtime errors or silent failures.
- gotcha The `waitForInitialization` method, if called without a timeout, will await indefinitely if the client fails to initialize (e.g., due to network issues or incorrect configuration) without an explicit error handler or rejection.
Install
-
npm install launchdarkly-js-client-sdk -
yarn add launchdarkly-js-client-sdk -
pnpm add launchdarkly-js-client-sdk
Imports
- initialize
const initialize = require('launchdarkly-js-client-sdk').initialize;import { initialize } from 'launchdarkly-js-client-sdk'; - LDClient
import LDClient from 'launchdarkly-js-client-sdk';
import { LDClient } from 'launchdarkly-js-client-sdk'; - LDUser
import { User } from 'launchdarkly-js-client-sdk';import { LDUser } from 'launchdarkly-js-client-sdk';
Quickstart
import { initialize, LDUser, LDClient, LDOptions } from 'launchdarkly-js-client-sdk';
// Replace with your actual Client-side ID from LaunchDarkly project settings
const LAUNCHDARKLY_CLIENT_SIDE_ID = process.env.LAUNCHDARKLY_CLIENT_SIDE_ID ?? 'YOUR_CLIENT_SIDE_ID';
// Define a user context. The 'key' is mandatory.
const user: LDUser = {
key: 'example-user-key',
name: 'Example User',
email: 'user@example.com',
custom: {
region: 'us-west'
}
};
// Define SDK options (optional, here enabling debug logging)
const options: LDOptions = {
debug: true,
};
// Initialize the LaunchDarkly client
const ldClient: LDClient = initialize(LAUNCHDARKLY_CLIENT_SIDE_ID, user, options);
ldClient.on('ready', () => {
console.log('LaunchDarkly client is ready!');
// Evaluate a boolean feature flag with a default value of `false`
const showNewFeature = ldClient.variation('new-feature-flag', false);
if (showNewFeature) {
console.log('The new feature is ENABLED for this user!');
} else {
console.log('The new feature is DISABLED for this user.');
}
// Track a custom event for analytics
ldClient.track('button-click', { buttonId: 'submit-form' });
});
ldClient.on('failed', (error) => {
console.error('LaunchDarkly initialization failed:', error);
});
// In a real application, you might export ldClient or make it globally available
// for other parts of your application to use.