# How to use Feature Flags in Node.js

DevFeed: [How to use Feature Flags in Node.js](<https://devfeed.tech/articles/how-to-use-feature-flags-in-node-js-24990.md>)

Original publisher: [Read original article](<https://codeahoy.com/2021/09/12/how-to-use-feature-flags-in-node-js/>)

Author: umer

Published: 2021-09-12T00:00:00Z

Content type: tutorial

Language: en

Sources: [Code Ahoy - Articles](<https://devfeed.tech/sources/code-ahoy-articles.md>)

Topics: [feature flags](<https://devfeed.tech/topics/feature-flags.md>), [Node.js](<https://devfeed.tech/topics/node-js.md>), [Code](<https://devfeed.tech/topics/code.md>), [Open Source](<https://devfeed.tech/topics/open-source.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [feature-flags](<https://devfeed.tech/tags/feature-flags.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [node-js](<https://devfeed.tech/tags/node-js.md>), [open-source](<https://devfeed.tech/tags/open-source.md>)

## AI overview

A tutorial on using feature flags in a Node.js application. It explains how to place a feature behind a flag, configure targeting rules, distribute a feature to a percentage of users, and evaluate the flag with a Node.js SDK. The referenced Unlaunch service has since shut down.

## Source excerpt

Note: Unlaunch, the feature flagging service referenced in this post, has been shut down. Links to it have been removed. Feature flags (or feature toggles) is a powerful technique used by modern software teams to control the behavior of their code and features in production. Feature flags are used for: rolling new features out gradually branch by abstraction experimentation and testing migrations geographic restrictions permissions or kill switch testing in production In this article, I'll show you how to use feature flag in Node.js. For this example, let's assume we have an e-commerce store and we're implementing a new feature to change the sort order of the items because in our hypothetical example, the marketing team thinks changing the sort order will lead to more revenue. I'll be using an open-source Node project called Crate which is an eCommerce subscription service for trendy clothes and accessories. 1. Put your feature behind feature flag in code First, you'll need to define the new feature in such a way it can be shown or hidden easily. if featureFlag == "on" then // show the new feature: new sort order else // show the old feature: old sort order Once you have wrapped your code (new and old features) in a feature flag, you can easily control the behavior by changing the state of the feature flag or its targeting rules. If the flag is enabled ("on" variation,) we show the new feature. Otherwise, we don't. 2. Create a feature flag Next, create a new feature flag in Unlaunch (Link Removed) that will control the feature you're building. You can call it the "default-sort-order" or "crate-products". Targeting Users A user is any object such as an email address, unique user id, a hash etc. to represent a unique user for which a feature flag is evaluated. Let's define an internal user (engineering, QA or product team member) by email address to always show the "on" variation so they always get the new feature. Targeting Rules The rules that define which variation