@harnessio/ff-nodejs-server-sdk

中文文档
1.8.13发布于 2026/10/07Apache-2.0TypeScriptESM + CJS近 30 天下载 8.9万

Feature flags SDK for NodeJS environments

原始使用文档
文档目录6 节
  1. Requirements
  2. Installing the SDK
  3. Code Sample
  4. Running the example
  5. Using custom TLS certificates
  6. Additional Reading

Node.js SDK For Harness Feature Flags

TypeScript version Node.js version APLv2

Use this README to get started with our Feature Flags (FF) SDK for Node.js. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out.

This sample doesn't include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the Node.js SDK Reference.

Requirements

To use this SDK, make sure you’ve:

  • Installed Node.js v12 or a newer version

To follow along with our test code sample, make sure you’ve:

Installing the SDK

The first step is to install the FF SDK as a dependency in your application. To install using npm, use:

npm install @harnessio/ff-nodejs-server-sdk

Or to install with yarn, use:

yarn add @harnessio/ff-nodejs-server-sdk

Code Sample

The following is a complete code example that you can use to test the harnessappdemodarkmode Flag you created on the Harness Platform. When you run the code it will:

  • Connect to the FF service.
  • Report the value of the Flag every 10 seconds until the connection is closed.
  • Listen for when the harnessappdemodarkmode Flag is toggled on or off on the Harness Platform and report the new value.
const { Client, Event } = require('@harnessio/ff-nodejs-server-sdk');

(async () => {
  // set apiKey to your SDK API Key
  const apiKey = 'YOUR_FF_SDK_KEY';

  // set flagName to your flag identifier from the UI
  const flagName = 'harnessappdemodarkmode';

  console.log('Harness SDK Getting Started');

  // Create Client
  const client = new Client(apiKey);

  // Create a target (different targets can receive different results based on rules.
  // Here we are including "location" as a custom attribute)
  const target = {
    identifier: 'nodeserversdk',
    name: 'NodeServerSDK',
    attributes: {
      location: 'emea',
    },
  };

  try {
    await client.waitForInitialization();
  } catch (e) {
    console.log("Error when authenticating Feature Flags client: " + e)
  }

  try {
    // Log the state of the flag every 10 seconds
    setInterval(async () => {
      const value = await client.boolVariation(flagName, target, false);
      console.log('Flag variation:', value);
    }, 10000);

    // We can also watch for the event when a flag changes
    client.on(Event.CHANGED, async (flagIdentifier) => {
      const value = await client.boolVariation(flagIdentifier, target, false);
      console.log(`${flagIdentifier} changed: ${value}`);
    });
  } catch (e) {
    console.error('Error:', e);
  }
})();

Running the example

To run the example, execute your script:

node example.js

When you're finished you can exit the example by stopping the process using control-c.

Using custom TLS certificates

If using a proxy or SMP instance that does not use a widely publicised pre-bundled CA you can provide your own CA using the tlsTrustedCa option. This option takes a path to a file that contains a bundle of certificates in PEM format. If this option is present, the HTTP client will ignore any pre-bundled certs so you need to include the entire certificate chain of your custom cert.

  const client = new Client(apiKey, {
    baseUrl: 'https://ffserver:8000/api/1.0',
    eventsUrl: 'https://ffserver:8001/api/1.0',
    tlsTrustedCa: 'path_to_cert_chain.crt',
  });

Additional Reading

For further examples and config options, see the Node.js SDK Reference and the test Node.js project. For more information about Feature Flags, see our Feature Flags documentation.

版本与发布记录

@harnessio/ff-nodejs-server-sdk 共发布 63 个版本,首次发布于 2021/10/15;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.8.13latest2026/10/071 个月前
1.8.122026/01/1210 个月前
1.8.112025/11/2611 个月前
1.8.102025/11/101 年前
1.8.92025/08/181.2 年前
1.8.82025/07/231.3 年前

其他发布通道:beta → 1.6.0-rc.2。

关于 @harnessio/ff-nodejs-server-sdk 的常见问题

@harnessio/ff-nodejs-server-sdk 是什么?

Feature flags SDK for NodeJS environments

如何安装 @harnessio/ff-nodejs-server-sdk?

在项目目录运行 npm install @harnessio/ff-nodejs-server-sdk,或使用 pnpm add / yarn add / bun add。

@harnessio/ff-nodejs-server-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@harnessio/ff-nodejs-server-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@harnessio/ff-nodejs-server-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@harnessio/ff-nodejs-server-sdk 可以商用吗?

@harnessio/ff-nodejs-server-sdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@harnessio/ff-nodejs-server-sdk 的下载量有多少?

根据 npm 官方下载统计,@harnessio/ff-nodejs-server-sdk 在 2026/10/18 – 2026/11/16 这 30 天内被下载 89,267 次,日均约 2,976 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@harnessio/ff-nodejs-server-sdk 还在维护吗?

最新版本 1.8.13 发布于 2026/10/07(1 个月前);至今共发布 63 个版本,其中过去 12 个月发布了 3 个;首次发布于 2021/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。