Node.js SDK reference (client-side)
Read time: 3 minutes
Last edited: May 12, 2023
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users."
Code samples on this page are from the two most recent SDK versions where they differ. To learn more about upgrading, read Node.js SDK 2.x to 3.0 migration guide.
Overview
This topic documents how to get started with the client-side Node.js SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | node-client-sdk |
Sample application | Node.js (client-side) |
Published module | npm |
This SDK is intended for use in single-user mobile, desktop, and embedded applications. If you have a Node.js application and want to set up LaunchDarkly on the server-side, read the server-side Node.js SDK reference. If you are using Electron, there is an Electron SDK more specifically designed for that environment.
To learn more about LaunchDarkly's different SDK types, read Client-side and server-side SDKs.
This SDK is closely related to the browser JavaScript SDK and has almost exactly the same API, but does not have any browser-specific functionality and adds several features specific to Node.
The sample code snippets for this SDK are available in both JavaScript and TypeScript, where the sample code differs. To learn more, read Using LaunchDarkly with TypeScript.
Getting started
After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your Node.js code.
First, install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager.
Here's how:
npm install launchdarkly-node-client-sdk
Next, import the LaunchDarkly client in your application code:
const LaunchDarkly = require('launchdarkly-node-client-sdk');
After you install and import the SDK, create a single, shared instance of LDClient
. To create a client instance, you need your environment's client-side ID. This authorizes your application to connect to a particular environment within LaunchDarkly.
To find and copy your LaunchDarkly client-side ID:
- Navigate to the Account settings page.
- Click the Projects tab.
- Click the name of your project. The Environments tab appears.
- Click the environment's client-side ID to copy it to your clipboard.
Client-side IDs are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
It's important to make LDClient
a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.
If you have multiple LaunchDarkly projects, you can create one LDClient
for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
Feature flag targeting and rollouts are determined by the active user. You must pass a user context to the SDK during initialization before requesting any feature flags with variation
. Failure to pass a valid user context to the SDK during initialization will result in an error.
Here's how to initialize the client:
const context = {kind: 'user',key: 'user-key-123abc'};const client = LaunchDarkly.initialize('client-side-id-123abc', context);
The client emits a ready
event when you have initialized it. You can also use the waitForInitialization()
method, which returns a Promise. After you have initialized it, you can safely call variation
to access your feature flags. The SDK emits the ready
event only once, when the client first initializes. In a production application, your calls to client.variation
would normally not be inside of this event handler.
To emit a ready
event:
client.on('ready', () => {// initialization succeeded, flag values are now availableconst flagValue = client.variation('flag-key-123abc', false);// etc.});
The SDK does not subscribe to streaming real-time updates automatically when you initialize it. Setting the streaming
option to true
in the client configuration causes the SDK to open a streaming connection to LaunchDarkly and receive live feature flag updates. You can also specify an event handler with client.on('change')
to be notified immediately when a flag changes.
You must make feature flags available to client-side SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the context will receive the fallback value for that flag.
To make a flag available to this SDK, check the SDKs using Client-side ID checkbox during flag creation, or on the flag's Settings tab. To make all of a project's flags available to this SDK by default, check the SDKs using Client-side ID checkbox in your project Settings.
Shutting down
Shut down the client when your application terminates. To learn more, read Shutting down.
Supported features
This SDK supports the following features:
- Anonymous contexts and users
- Bootstrapping
- Configuration, including
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Private attributes
- Relay Proxy configuration, using proxy mode
- Sending custom events
- Shutting down
- Subscribing to flag changes
- User and context configuration