npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nuxt-feature-flags

v1.1.7

Published

Feature flags for Nuxt with A/B testing and variant support

Readme

nuxt-feature-flags

npm version npm downloads License Nuxt

Nuxt Feature Flags 🚩

A powerful, type-safe feature flag module for Nuxt 3 with A/B testing and variant support.

✨ Features

  • 🎯 Context-aware evaluation - Evaluate flags based on user, device, environment
  • 🛠 TypeScript Ready - Full type safety with autocomplete
  • 🧩 Nuxt 3 Integration - Seamless integration with auto-imports
  • 🔀 A/B/n Testing - Built-in variant support with persistent assignment
  • Validateion - Built-in validation for flag configuration
  • 🔒 Type Safety - Catch errors early with full type inference

📦 Installation

npx nuxi module add nuxt-feature-flags

🚀 Quick Start

1. Add to your Nuxt config

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-feature-flags']
})

2. Configure your flags

// feature-flags.config.ts
import { defineFeatureFlags } from '#feature-flags/handler'

export default defineFeatureFlags(() => ({
  newDashboard: true,
  darkMode: false,
  
  // A/B test with variants
  buttonDesign: {
    enabled: true,
    variants: [
      { name: 'control', weight: 50, value: 'blue' },
      { name: 'treatment', weight: 50, value: 'red' }
    ]
  }
}))
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-feature-flags'],
  featureFlags: {
    config: './feature-flags.config.ts'
  }
})

3. Use in your app

In components:

<script setup>
const { isEnabled, getValue, getVariant } = useFeatureFlags()
</script>

<template>
  <div>
    <!-- Simple flag check -->
    <NewDashboard v-if="isEnabled('newDashboard')" />
    
    <!-- Using directive -->
    <div v-feature="'darkMode'">Dark mode content</div>
    
    <!-- A/B test variants -->
    <button :class="`btn-${getValue('buttonDesign')}`">
      Click me
    </button>
  </div>
</template>

In server routes:

// server/api/data.ts
export default defineEventHandler((event) => {
  const { isEnabled, getValue } = getFeatureFlags(event)
  
  if (!isEnabled('newDashboard')) {
    throw createError({ statusCode: 404 })
  }
  
  return { data: 'Dashboard data' }
})

🎯 Context-Aware Flags

Create dynamic flags based on user, device, or environment:

// feature-flags.config.ts
export default defineFeatureFlags((context) => ({
  // Role-based
  adminPanel: context?.user?.role === 'admin',
  
  // Environment-based
  devTools: process.env.NODE_ENV === 'development',
  
  // Device-based
  mobileUI: context?.device?.isMobile ?? false,
  
  // Gradual rollout (20% of users)
  newFeature: {
    enabled: true,
    variants: [
      { name: 'old', weight: 80, value: false },
      { name: 'new', weight: 20, value: true }
    ]
  }
}))

Populate context in server middleware:

// server/middleware/user-context.ts
export default defineEventHandler(async (event) => {
  const user = await getUserFromSession(event)
  
  if (user) {
    event.context.user = {
      id: user.id,
      role: user.role
    }
  }
})

📖 API Reference

Client-Side

const { 
  flags,              // Ref<ResolvedFlags>
  isEnabled,          // (flag: string, variant?: string) => boolean
  getVariant,         // (flag: string) => string | undefined
  getValue,           // (flag: string) => any
  getFlag,            // (flag: string) => ResolvedFlag | undefined
  fetch               // () => Promise<void>
} = useFeatureFlags()

Server-Side

const { 
  flags,              // ResolvedFlags
  isEnabled,          // (flag: string, variant?: string) => boolean
  getVariant,         // (flag: string) => string | undefined
  getValue,           // (flag: string) => any
  getFlag             // (flag: string) => ResolvedFlag | undefined
} = getFeatureFlags(event)

Template Directive

<template>
  <!-- Show if flag is enabled -->
  <div v-feature="'myFlag'">Content</div>
  
  <!-- Show for specific variant -->
  <div v-feature="'myFlag:variantA'">Variant A content</div>
</template>

✅ Validation

Validate flags at build time:

// scripts/validate-flags.ts
import { validateFeatureFlags } from 'nuxt-feature-flags/build'

const errors = await validateFeatureFlags({
  configPath: './feature-flags.config.ts',
  srcPatterns: ['**/*.vue', '**/*.ts'],
  failOnErrors: true
})

📚 Documentation

For detailed documentation, visit nuxt-feature-flags-docs.vercel.app

🤝 Contributing

Contributions are welcome! Please read our contributing guide.

📄 License

MIT License © 2024