# Vanilla JavaScript


Use this guide when you want to install Flare directly in a plain JavaScript application without a framework-specific integration layer.

> Laravel Mix is deprecated. We recommend Vite for new projects.

> This guide covers every supported setup. Follow only the steps that match your project: conditional instructions start with a bold "If ..." line describing when they apply. Steps without such a line always apply.
> Install commands use `npm install`. Swap that prefix for the project's own package manager: `yarn add`, `pnpm add`, or `bun add`.
> Replace `PROJECT PUBLIC KEY` with the project's public key. You can find it in the project settings on https://flareapp.io.

## Step 1: Install the package using NPM, Yarn, pnpm, or Bun

**If you don't upload sourcemaps to Flare:**

```shell
npm install @flareapp/js
```

**If you use Vite and upload sourcemaps to Flare:**

```shell
npm install @flareapp/js @flareapp/vite
```

**If you use Webpack and upload sourcemaps to Flare:**

```shell
npm install @flareapp/js @flareapp/webpack
```

**If you use Next.js and upload sourcemaps to Flare:**

```shell
npm install @flareapp/js @flareapp/nextjs
```

## Step 2: Register Flare & configure

Initialize Flare in your `app.js` or `index.js` file:

**If you upload sourcemaps to Flare:**

In `app.js`:

```javascript
import { flare } from "@flareapp/js";

// Only enable Flare in production, we don't want to waste your quota while you're developing:
if (process.env.NODE_ENV === 'production') {
    flare.light();
}
```

**If you don't upload sourcemaps to Flare:**

In `app.js`:

```javascript
import { flare } from "@flareapp/js";

// Only enable Flare in production, we don't want to waste your quota while you're developing:
if (process.env.NODE_ENV === 'production') {
    flare.light('PROJECT PUBLIC KEY');
}
```

**If you enable tracing:**

Tracing shows you where time goes during a page load, a navigation, and the requests your page makes. Add this inside the same production check, after `flare.light()`. Read more in [how tracing works](/docs/javascript/tracing/how-tracing-works) and [sampling](/docs/javascript/tracing/sampling).

In `app.js`:

```javascript
flare.configure({
    enableTracing: true,
    // Sends 10% of traces. Raise it for more detail, lower it to save quota.
    tracesSampleRate: 0.1,
});
```

**If you use Vite and upload sourcemaps to Flare:**

Configure Vite to upload sourcemaps to Flare:

In `vite.config.js`:

```javascript
import { defineConfig } from 'vite';
import flareSourcemaps from '@flareapp/vite';

export default defineConfig({
    plugins: [
        flareSourcemaps({
            apiKey: 'PROJECT PUBLIC KEY'
        }),
    ],
});
```

**If you use Webpack and upload sourcemaps to Flare:**

Configure Webpack to upload sourcemaps to Flare:

In `webpack.config.js`:

```javascript
const { FlareWebpackPlugin } = require("@flareapp/webpack");

module.exports = {
    // ...
    devtool: "source-map",
    plugins: [ new FlareWebpackPlugin({ apiKey: "PROJECT PUBLIC KEY" }) ],
};
```

**If you use Next.js and upload sourcemaps to Flare:**

Wrap your Next.js config with `withFlareSourcemaps` in `next.config.mjs`:

In `next.config.mjs`:

```javascript
import { withFlareSourcemaps } from '@flareapp/nextjs';

export default withFlareSourcemaps({
    // your normal Next.js config
}, {
    apiKey: 'PROJECT PUBLIC KEY',
});
```

## Step 3: Connect your project key

**If you upload sourcemaps to Flare:**

When you install Flare's sourcemap build plugin, the build plugin injects your public key so `flare.light()` can run without an explicit key. Make sure you add the domains of your application in the project settings on Flare.

**If you don't upload sourcemaps to Flare:**

Pass your public key directly to `flare.light('PROJECT PUBLIC KEY')` and make sure you add the domains of your application in the project settings on Flare.

## Step 4: Verify your setup

Add this code temporarily to verify everything is working:

In `app.js`:

```javascript
flare.test();
```

**If you use Vite and upload sourcemaps to Flare:**

Run your Vite build command. You should see the following lines in the output:

```shell
@flareapp/vite: Uploading xx sourcemap(s) to Flare.
@flareapp/vite: Successfully uploaded all sourcemaps to Flare.
```

**If you use Next.js and upload sourcemaps to Flare:**

Run `next build`. You should see the following lines in the output:

```shell
@flareapp/webpack: Uploading 4 sourcemap(s) to Flare.
@flareapp/webpack: Successfully uploaded all sourcemaps to Flare.
```

## Sourcemaps

If your application is bundled for production, configure sourcemap uploads so Flare can show readable stack traces and code snippets. Start with the guide above for the common setup path, and use the [resolving bundled code guide](/docs/javascript/errors/sourcemaps) for advanced bundler configuration or manual upload options.

## Troubleshooting

If you need more insight while setting things up, enable debug mode with `flare.light('key', true)` or by setting `flare.debug = true` before reporting an error.
