Unleash Node.js Client
The `unleash-client` package provides a server-side SDK for Node.js applications to integrate with the Unleash feature management platform. It enables dynamic evaluation of feature flags, allowing developers to control feature rollouts, perform A/B testing, and manage configurations without redeploying code. The current stable version is 6.11.1, with frequent patch and minor releases addressing bug fixes, performance improvements, and new constraint types (like semver and CIDR). This client is designed for high performance and low overhead, maintaining an in-memory repository of feature toggles that are polled from the Unleash server at regular intervals. It supports both Unleash Enterprise and Open Source instances and is suitable for applications requiring robust and scalable feature flag capabilities. Key differentiators include its focus on server-side evaluation, asynchronous initialization with event-driven readiness signals, and support for various constraint types.
Common errors
-
Error: Cannot find module 'unleash-client' or Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS environment without proper configuration, or the package is not installed.fixEnsure `unleash-client` is installed (`npm install unleash-client`). If using CommonJS, change `import { initialize } from 'unleash-client';` to `const { initialize } = require('unleash-client');`. -
unleash.isEnabled is not a function
cause The Unleash client object (returned by `initialize` or `new UnleashClient()`) has not been correctly instantiated or is out of scope.fixVerify that `unleash` is a valid `UnleashClient` instance. Ensure it's correctly assigned from `initialize()` and accessible in the scope where `isEnabled` is called. -
TypeError: Invalid URL
cause The `url` option provided to `initialize` is not a valid URL format.fixEnsure the `url` parameter in `initialize({ url: '...' })` is a complete and valid URL, e.g., 'https://unleash.example.com/api'. -
Feature flag 'my-feature' is always disabled, even when enabled in Unleash UI.
cause The Unleash client might not have successfully connected and synchronized with the Unleash server, or the `appName` and `instanceId` do not match the configuration in Unleash UI.fixCheck the client's logs for connection errors. Ensure `appName` and `instanceId` match the client registration in Unleash. Wait for the `'synchronized'` event before relying on `isEnabled()` outcomes. Verify the provided API token has client access permissions.
Warnings
- breaking The minimum required Node.js version is now 20. Applications running on older Node.js versions will fail to start or encounter runtime errors.
- gotcha By default, `unleash-client` initializes asynchronously. Before the client has successfully synchronized with the Unleash server, all feature evaluations will return `false`. This can lead to unexpected behavior if not handled correctly.
- gotcha Calling `initialize()` multiple times will reconfigure the *global* Unleash instance. It does not create multiple distinct instances. Similarly, directly constructing `new UnleashClient()` multiple times without managing lifecycles can lead to redundant connections and increased load on the Unleash API.
- gotcha Security vulnerability detected in `minimatch` dependency (CVE-2023-38546). While mitigated in version `v9.0.7`, older versions of `unleash-client` using vulnerable `minimatch` versions could be susceptible.
- breaking Prior to v6.9.4, `make-fetch-happen` was on an older dependency version. Upgrading this dependency introduced potential breaking changes due to its own major version bumps, particularly around request options or caching behaviors.
Install
-
npm install unleash-client -
yarn add unleash-client -
pnpm add unleash-client
Imports
- initialize
const initialize = require('unleash-client').initialize;import { initialize } from 'unleash-client'; - UnleashClient
const UnleashClient = require('unleash-client').UnleashClient;import { UnleashClient } from 'unleash-client'; - Unleash
import { Unleash } from 'unleash-client';
Quickstart
import { initialize } from 'unleash-client';
const unleash = initialize({
url: 'https://unleash.example.com/api',
appName: 'my-node-app',
instanceId: 'my-unique-instance-id',
customHeaders: { Authorization: process.env.UNLEASH_API_TOKEN ?? '' },
refreshInterval: 15000 // Poll for updates every 15 seconds
});
unleash.on('error', console.error);
unleash.on('warn', console.warn);
unleash.on('ready', () => {
console.log('Unleash client is ready!');
});
unleash.on('synchronized', () => {
console.log('Unleash client has synchronized with the server.');
if (unleash.isEnabled('my-feature-flag')) {
console.log('Feature \'my-feature-flag\' is enabled!');
} else {
console.log('Feature \'my-feature-flag\' is disabled.');
}
});
// Example of usage after initialization (ensure client is ready first)
setTimeout(() => {
if (unleash.isEnabled('another-feature', { userId: '123' })) {
console.log('Another feature is enabled for user 123.');
} else {
console.log('Another feature is disabled for user 123.');
}
}, 5000);