Skip to content

Installation ​

How to add CookieConsent to an app that uses npm and a bundler, such as React, Vue, Next.js or Angular. For a plain HTML website, follow Getting Started instead.

Steps 3 and 4 have a tab for each framework. The other steps are the same everywhere.

1. Install the package ​

sh
npm i vanilla-cookieconsent@3.3.1
sh
pnpm add vanilla-cookieconsent@3.3.1
sh
yarn add vanilla-cookieconsent@3.3.1
sh
bun add vanilla-cookieconsent@3.3.1

2. Create the config file ​

Create cookieconsent-config.js. It loads the stylesheet and exports a function that starts the plugin. See the Configuration reference for every option:

javascript
import 'vanilla-cookieconsent/dist/cookieconsent.css';
import { run } from 'vanilla-cookieconsent';

export const runCookieConsent = () => {
    return run({
        categories: {
            necessary: {
                enabled: true,  // this category is enabled by default
                readOnly: true  // this category cannot be disabled
            },
            analytics: {}
        },

        language: {
            default: 'en',
            translations: {
                en: {
                    consentModal: {
                        title: 'We use cookies',
                        description: 'We use cookies to make the site work and, with your consent, to understand how you use it.',
                        acceptAllBtn: 'Accept all',
                        acceptNecessaryBtn: 'Reject all',
                        showPreferencesBtn: 'Manage preferences'
                    },
                    preferencesModal: {
                        title: 'Cookie preferences',
                        acceptAllBtn: 'Accept all',
                        acceptNecessaryBtn: 'Reject all',
                        savePreferencesBtn: 'Save preferences',
                        closeIconLabel: 'Close',
                        sections: [
                            {
                                title: 'Strictly necessary cookies',
                                description: 'These cookies are essential for the website to work and cannot be disabled.',
                                linkedCategory: 'necessary'
                            },
                            {
                                title: 'Performance and analytics',
                                description: 'These cookies collect information about how you use our website.',
                                linkedCategory: 'analytics'
                            }
                        ]
                    }
                }
            }
        }
    });
};

Name it cookieconsent-config.ts in a TypeScript project: the code is the same. Step 3 shows where each framework keeps it.

3. Run it in the browser ​

Call runCookieConsent() once, after your app has rendered in the browser:

jsx
// src/App.jsx (config in src/cookieconsent-config.js)
import { useEffect } from 'react';
import { runCookieConsent } from './cookieconsent-config';

export default function App() {
    useEffect(() => {
        // StrictMode runs this twice in development: the plugin still starts once
        runCookieConsent();
    }, []);

    // ...
}
jsx
// app/CookieConsentBanner.jsx (config in app/cookieconsent-config.js)
// With a src/ folder, use src/app/
'use client';

import { useEffect } from 'react';
import { runCookieConsent } from './cookieconsent-config';

export default function CookieConsentBanner() {
    useEffect(() => {
        runCookieConsent();
    }, []);

    return null;
}

// app/layout.jsx: render it in your root layout
import CookieConsentBanner from './CookieConsentBanner';

// ...
<body>
    {children}
    <CookieConsentBanner />
</body>
vue
<!-- src/App.vue (config in src/cookieconsent-config.js) -->
<!-- In TypeScript projects, use <script setup lang="ts"> -->
<script setup>
import { onMounted } from 'vue';
import { runCookieConsent } from './cookieconsent-config';

onMounted(() => {
    runCookieConsent();
});
</script>
javascript
// app/plugins/cookieconsent.client.js (config in app/cookieconsent-config.js)
// The .client suffix runs it only in the browser.
// Nuxt 3 without an app/ folder: use plugins/, and put the config in the project root
import { runCookieConsent } from '~/cookieconsent-config';

export default defineNuxtPlugin(() => {
    runCookieConsent();
});
svelte
<!-- src/routes/+layout.svelte (config in src/lib/cookieconsent-config.js) -->
<!-- Without SvelteKit, call it from onMount in src/App.svelte -->
<script>
    import { onMount } from 'svelte';
    import { runCookieConsent } from '../lib/cookieconsent-config';

    let { children } = $props();

    onMount(() => {
        runCookieConsent();
    });
</script>

{@render children()}
jsx
// src/App.jsx (config in src/cookieconsent-config.js)
import { onSettled } from 'solid-js'; // Solid 1.x: onMount
import { runCookieConsent } from './cookieconsent-config';

export default function App() {
    onSettled(() => {
        runCookieConsent();
    });

    // ...
}
astro
<!-- src/layouts/Layout.astro (config in src/cookieconsent-config.js) -->
<!-- Astro bundles the script and runs it once per page load -->
<body>
    <slot />

    <!-- Only with <ClientRouter />: Astro replaces the <body> on every page change.
         Keep the modals in this element, and add root: '#cc-root' to your config. -->
    <div id="cc-root" transition:persist></div>

    <script>
        import { runCookieConsent } from '../cookieconsent-config';
        runCookieConsent();
    </script>
</body>
typescript
// src/app/app.ts, or app.component.ts before Angular 20
// Config in src/app/cookieconsent-config.ts, without the CSS import:
// Angular loads the stylesheet from src/styles.css instead:
// @import 'vanilla-cookieconsent/dist/cookieconsent.css';
import { Component, afterNextRender } from '@angular/core';
import { runCookieConsent } from './cookieconsent-config';

@Component({ /* ... */ })
export class App {
    constructor() {
        // never runs on the server, so this works with SSR (Angular 17+)
        afterNextRender(() => {
            runCookieConsent();
        });
    }
}

4. Add a preferences button ​

Let users change their choice later, for example from a link in your footer:

jsx
import { showPreferences } from 'vanilla-cookieconsent';

// ...
<button type="button" onClick={() => showPreferences()}>
    Cookie preferences
</button>
jsx
// needs 'use client', like any component with an onClick
'use client';

import { showPreferences } from 'vanilla-cookieconsent';

// ...
<button type="button" onClick={() => showPreferences()}>
    Cookie preferences
</button>
vue
<script setup>
import { showPreferences } from 'vanilla-cookieconsent';
</script>

<template>
    <button type="button" @click="showPreferences()">
        Cookie preferences
    </button>
</template>
vue
<script setup>
import { showPreferences } from 'vanilla-cookieconsent';
</script>

<template>
    <button type="button" @click="showPreferences()">
        Cookie preferences
    </button>
</template>
svelte
<script>
    import { showPreferences } from 'vanilla-cookieconsent';
</script>

<button type="button" onclick={() => showPreferences()}>
    Cookie preferences
</button>
jsx
import { showPreferences } from 'vanilla-cookieconsent';

// ...
<button type="button" onClick={() => showPreferences()}>
    Cookie preferences
</button>
html
<!-- Astro pages are plain HTML, so the data-cc attribute works.
     With <ClientRouter />, put it inside an element with transition:persist. -->
<button type="button" data-cc="show-preferencesModal">Cookie preferences</button>
typescript
import { Component } from '@angular/core';
import { showPreferences } from 'vanilla-cookieconsent';

@Component({
    selector: 'app-footer',
    template: `<button type="button" (click)="openPreferences()">Cookie preferences</button>`
})
export class Footer {
    openPreferences() {
        showPreferences();
    }
}

Showing the banner is not enough: scripts that set cookies must not run until the user accepts them. In an app, the simplest way is to load them from the onConsent and onChange callbacks:

javascript
import 'vanilla-cookieconsent/dist/cookieconsent.css';
import { run, acceptedCategory, loadScript } from 'vanilla-cookieconsent';

const loadAnalytics = () => {
    if (acceptedCategory('analytics'))
        loadScript('https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX');
};

export const runCookieConsent = () => {
    return run({
        onConsent: loadAnalytics,
        onChange: loadAnalytics,
        // ... the rest of your config
    });
};

onConsent runs on every page load once the user has made a choice, and onChange when they change it later. loadScript() adds each script only once. Script tags in your HTML can be blocked too: see Scripts management.

Released under the MIT License.