SDK quick linksLaunchDarkly’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 |
| Supported SDK Versions | Node.js client SDK |
| GitHub repository | node-client-sdk |
| Sample application | Node.js (client-side) |
| Published module | npm |
For use in mobile, desktop, and embedded client applications onlyThis 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 Choosing an SDK type.
Get started
After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your Node.js code:Install the SDK
First, install the LaunchDarkly SDK as a dependency in your application using your application’s dependency manager. Here’s how:@launchdarkly/node-client-sdk. In v3.x of the SDK, the package was named launchdarkly-node-client-sdk.
Initialize the client
After you install and import the SDK, create a single, shared instance ofLDClient. To create a client instance, you need your environment’s client-side ID and the context for which you want to evaluate flags. This authorizes your application to connect to a particular environment within LaunchDarkly.
Feature flag targeting and rollouts are determined by the active context, which describes the end user using your application. You must pass a context to the SDK during initialization before requesting any feature flags with variation. Failure to pass a valid context to the SDK during initialization will result in an error.
When you initialize the client, use the waitForInitialization() method to determine if the client successfully initializes. The waitForInitialization() method takes a timeout, which we recommend setting to five seconds or fewer. If you use a large timeout and await it, then any network delays will cause your application to wait a long time before continuing execution.
Here’s how to initialize the client:
waitForInitialization() call, you can use event listeners instead. In both versions, the client emits "initialized" to indicate success and "ready" when it has finished starting up, regardless of whether it successfully connected to LaunchDarkly. In the Node.js SDK v3.x, the client also emits "failed" to indicate failure. In the Node.js SDK v4.x, failures are reported through the "error" event instead. In a production application, your calls to client.variation would normally not be inside of this event handler.
To use event listeners:
LDOptions.
After you have initialized the client, you can safely call variation to access your feature flags.
Subscribe to flag changes
The SDK does not subscribe to streaming real-time updates automatically when you initialize it. In some cases, streaming may not be necessary. For example, if you reload your entire application on each update, you will get all the flag values again when the client is re-initialized. If this is your use case, you should leave thestreaming value undefined, which is the default.
In other cases, streaming may be required. Subscribing to streaming is the only way to receive real-time updates. If you determine that streaming is necessary for your application, there are two ways to subscribe to streaming:
- Explicitly subscribe to streaming: If you set the
streamingconfiguration option totrue, the client will always attempt to maintain a streaming connection. - Register a change listener: If you specify an event handler with
client.on('change')the client will open a streaming connection. It will close this streaming connection when you unsubscribe from the event, for example by callingclient.off('change'). Because opening and closing streaming connections can be expensive, you should explicitly enable streaming if your application frequently starts and stops listening to changes.
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.Troubleshooting
If your application logs show the errorLaunchDarklyFlagFetchError: network error, it may indicate a problem with network connectivity between your SDK and LaunchDarkly.
For steps to resolve this issue, read the LaunchDarkly Knowledge Base article Error “LaunchDarklyFlagFetchError: network error”.
Supported features
This SDK supports the following features:- Anonymous contexts and users
- Bootstrapping
- Caching contexts to local storage
- SDK configuration, including
- Context configuration
- Flag variation evaluation
- Flag variation evaluation details
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Private attributes
- Relay Proxy configuration, using proxy mode
- Secure mode
- Tracking custom events
- Shutting down
- Subscribing to flag changes