Vue.js 3.x integration guide
Typical installation time: 5 minutes
Hi there! You’ve found Honeybadger’s guide to Vue.js 3.x error and exception tracking. Once installed, Honeybadger will automatically report errors from your Vue.js application.
Installation
Section titled “Installation”Add @honeybadger-io/js and @honeybadger-io/vue as dependencies and configure.
# npmnpm add @honeybadger-io/js @honeybadger-io/vue --save
# yarnyarn add @honeybadger-io/js @honeybadger-io/vueIn your main.js (or main.ts):
import HoneybadgerVue from "@honeybadger-io/vue";import { createApp } from "vue";import App from "./App"; //your root component
const app = createApp(App);
const config = { apiKey: "PROJECT_API_KEY", environment: "production", revision: "git SHA/project version",};
app.use(HoneybadgerVue, config);
app.mount("#app");Using Vite for development
Section titled “Using Vite for development”If you are using Vite for local development, you may get CORS errors in your browser console. To work around that, you can apply the following in your vite.config.js (or vite.config.ts):
export default defineConfig({ // ... server: { cors: false, },});Reporting errors
Section titled “Reporting errors”Using the example configuration above, you’ll install @honeybadger-io/vue as Vue’s error handler.
By default, an error handler for all JavaScript errors will be attached to the
window.onerror handler for JavaScript errors that may originate from Vue
components or other JavaScript on the page.
Because Vue doesn’t intercept all errors that may occur within a Vue component,
errors that bubble up to the window.onerror handler may be missing some Vue
component contextual information, but the stack trace will be available.
If, for some reason, you do not wish to install Honeybadger’s error handler on
the global window.onerror handler, you may add { enableUncaught: false } to
the configuration when you’re registering HoneybadgerVue.
You may also manually report errors by directly invoking the honeybadger.js API.
Composition API
Section titled “Composition API”To access the Honeybadger instance when using the Composition API, use the
useHoneybadger function:
<script setup> import {useHoneybadger} from '@honeybadger-io/vue' const honeybadger = useHoneybadger() function notify() {honeybadger.notify("Hello from Vue 3!")}</script>Options API
Section titled “Options API”To access the Honeybadger instance when using the Options API, use
this.$honeybadger:
// inside a componentthis.$honeybadger.notify(error);See the full documentation for more options on how to
call notify().
Identifying users
Section titled “Identifying users”Honeybadger can track which users have encountered each error. To identify the
current user in error reports, add a user identifier and/or email address with
$honeybadger.setContext:
// inside a componentthis.$honeybadger.setContext({ user_id: 123, user_email: "user@example.com",});Sending additional context
Section titled “Sending additional context”Sometimes additional application state may be helpful for diagnosing errors. You
can arbitrarily specify additional key/value pairs when you invoke setContext.
// inside a componentthis.$honeybadger.setContext({ active_organization: 55, custom_configuration: false,});Clearing context
Section titled “Clearing context”If your user logs out or if your context changes during the Vue component
lifetime, you can set new values as appropriate by invoking setContext again.
Additionally, if needed, you can clear the context by invoking clear:
// inside a componentthis.$honeybadger.clear();Advanced usage
Section titled “Advanced usage”@honeybadger-io/vue is built on
honeybadger.js. Most
configuration options can be passed in to the config object you pass when
registering the HoneybadgerVue component with your Vue app instance. As of
this release, there are no Vue-specific configuration options, but that may
change as we learn more about Vue users’ unique needs.
In general, configuration and context options supported by the JavaScript
version of the library should work as is, aside from needing to reference
this.$honeybadger (or app.$honeybadger if you have access to your vue app
instance) instead of a global Honeybadger variable.
See the Honeybadger JavaScript integration documentation for additional customization options.
Tracking deploys
Section titled “Tracking deploys”As with vanilla JavaScript applications, you can notify Honeybadger when you’ve
deployed a new build. Honeybadger will associate an error report with a specific
revision number (matching the revision field in the configuration when
registering the Honeybadger component).
Here’s a simple curl script to record a deployment:
HONEYBADGER_ENV="production" \HONEYBADGER_REVISION="$(git rev-parse HEAD)" \HONEYBADGER_REPOSITORY="$(git config --get remote.origin.url)" \HONEYBADGER_API_KEY="Your project API key" \ && curl -g "https://api.honeybadger.io/v1/deploys?deploy[environment]=$HONEYBADGER_ENV&deploy[local_username]=$USER&deploy[revision]=$HONEYBADGER_REVISION&deploy[repository]=$HONEYBADGER_REPOSITORY&api_key=$HONEYBADGER_API_KEY"Be sure that the same revision is also configured in the @honeybadger-io/vue library. Read more about deploy tracking in the API docs.
Tracking deploys from Netlify
Section titled “Tracking deploys from Netlify”If you are deploying your site to Netlify, you can notify Honeybadger of
deployments via Netlify’s webhooks. Go to the Deploy notifications section
of the Build & deploy tab for your site settings, and choose to add an
Outgoing webhook notification. Choose Deploy succeeded as the event to listen
for, and use this format for your URL:
https://api.honeybadger.io/v1/deploys/netlify?api_key=YOUR_HONEYBADGER_API_KEY_HERE
The environment that will be reported to Honeybadger defaults to the Netlify
environment that was deployed, but you can override that with
&environment=CUSTOM_ENV in the webhook URL, if you like.
Source map support
Section titled “Source map support”Honeybadger can automatically un-minify your code if you provide a source map along with your minified JavaScript files. See our Source Map Guide for details.
Collect user feedback
Section titled “Collect user feedback”When an error occurs, a form can be shown to gather feedback from your users. Read more about this feature here.
Sample applications
Section titled “Sample applications”Two sample applications are included in the examples/ folder in the
honeybadger-vue repository, one for vue 2.x and one for vue 3.x.
You can follow the README.md inside each app to run them.
To create your own standalone Vue application, simply follow the Quick Start guide in Vue.js documentation. Remember to install Honeybadger Vue:
npm add @honeybadger-io/js @honeybadger-io/vueThen, in your main.js, you can follow the pattern in the source code in
examples/vue3/src/main.js:
import { createApp } from "vue";import App from "./App";import router from "./router";import HoneyBadgerVue from "@honeybadger-io/vue";
const app = createApp(App);app.use(HoneyBadgerVue, { apiKey: "your_api_key" });app.use(router).mount("#app");