With a Bundler for Node.js

Learn how to run your app with Sentry using a bundler plugin, without the --import flag.

If you build your server with a bundler, you can instrument your dependencies at build time with the Sentry bundler plugin instead of loading Sentry with the --import flag. Because the instrumentation is baked into your bundle, a plain top-level import of your instrument file is enough, and you can start your app with a regular node command, without the --import flag.

Use this method if you can't pass a command line flag to the Node.js binary, for example when your start command is fixed by your platform or process manager.

Create a file named instrument.(js|mjs) in the root directory of your project that initializes Sentry:

instrument.js
Copied
const Sentry = require("@sentry/node");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
Sentry.init({
  dsn: "___PUBLIC_DSN___",

  integrations: [
    // Add our Profiling integration
    nodeProfilingIntegration(),
  ],

  // Enables Tracing.
  // Set to 1.0 to capture 100% of spans for tracing.
  // We recommend reducing this value in production.
  tracesSampleRate: 1.0,

  // Enables Profiling for every session.
  profileSessionSampleRate: 1.0,
});

Add the Sentry plugin for your bundler to your build configuration. The plugin instruments your dependencies at build time, so the SDK no longer depends on Sentry.init() running before your other imports.

vite.config.mjs
Copied
import { sentryVitePlugin } from "@sentry/node/vite";

export default {
  plugins: [sentryVitePlugin()],
};

Import instrument.(js|mjs) at the very top of your application's entry point, before any other imports:

app.mjs
Copied
// Import this first!
import "./instrument.mjs";

// Now import other modules
import express from "express";

// Your application code goes here

Start your application as usual. No --import flag is required, because the instrumentation is already part of your bundle:

Copied
node app.mjs

If you can't use a bundler, review our other installation methods for alternatives that don't rely on the --import flag.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").