With a Bundler for Node.js
Learn how to run your app with Sentry using a bundler plugin, without the --import flag.
Are you unsure if you should use this installation method? Review our installation methods.
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.jsconst 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,
});
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,
});
import * as Sentry from "@sentry/node";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
// Ensure to call this before importing any other modules!
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.mjsimport { sentryVitePlugin } from "@sentry/node/vite";
export default {
plugins: [sentryVitePlugin()],
};
import { sentryVitePlugin } from "@sentry/node/vite";
export default {
plugins: [sentryVitePlugin()],
};
import { sentryRollupPlugin } from "@sentry/node/rollup";
export default {
plugins: [sentryRollupPlugin()],
};
import { sentryWebpackPlugin } from "@sentry/node/webpack";
export default {
plugins: [sentryWebpackPlugin()],
};
import { sentryEsbuildPlugin } from "@sentry/node/esbuild";
await esbuild.build({
plugins: [sentryEsbuildPlugin()],
});
Import instrument.(js|mjs) at the very top of your application's entry point, before any other imports:
app.mjs// Import this first!
import "./instrument.mjs";
// Now import other modules
import express from "express";
// Your application code goes here
// 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:
node app.mjs
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.
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").