Setting Up Datadog APM for Node.js - Load It First
Node.js APM succeeds or fails on whether the dd-trace SDK is loaded before every other module. Add --require for CommonJS or --import for ESM, and if you bundle with esbuild or Webpack, decide in advance on the dedicated plugin and how to handle native modules.
Why Load dd-trace Before Every Other Module?
dd-trace, the APM SDK for Node.js, works by intercepting the require() calls through which the app loads modules and injecting instrumentation code into modules such as express and pg as they are loaded. For that reason the SDK has to be loaded and initialized before any other module, and the official documentation builds its steps on that order. The symptoms listed are three: the URL route is missing from web requests, spans are cut off partway, and spans are missing. When these appear, the documentation advises you to first check where loading and initialization happen (as of October 2026; source: Tracing Node.js Applications). There are three ways to load it first in a CommonJS app, and they differ in where you pass the configuration and whether you can modify the startup command.
| Form | How configuration is passed | Suited to |
|---|---|---|
| node --require dd-trace/init app.js | All through environment variables | You can write the startup command yourself |
| node --require ./dd-trace.js app.js | Written in code in init() in a dedicated file | You want to build the configuration in code |
| NODE_OPTIONS='--require dd-trace/init' | All through environment variables | You cannot touch the startup arguments, or you want it to apply to child Node.js processes too |
In the second form, you prepare a file containing only the single statement require('dd-trace').init() and pass a configuration object as its argument. The idea is the same for setups that insert a transpilation step, such as TypeScript or Babel; the official documentation shows separating SDK initialization into its own file and loading that whole file at build time. One more thing to keep in mind is that DD_TRACE_ENABLED defaults to true, and part of the instrumentation runs at import time, before initialization. In environments where you want to stop instrumentation completely, load the SDK conditionally, or, where you cannot branch on a condition, as with static ESM imports, pass DD_TRACE_ENABLED=false.
ESM Apps Add --import, and the Argument Changes at Node.js v20.6
An app written in ECMAScript Modules (ESM) adds one loader argument, whichever form you use to load the SDK. The argument depends on the Node.js version. Before v20.6 it is --loader dd-trace/loader-hook.mjs. From v20.6 on it is --import dd-trace/register.js (as of October 2026; source: Tracing Node.js Applications). The loader can be written alongside --require dd-trace/init, and from v20.6 on you can simply pass the single dd-trace/initialize.mjs, which combines the two.
node --import dd-trace/register.js --require dd-trace/init app.js
node --import dd-trace/initialize.mjs app.js- 1. Is the app CommonJS or ESM - for CommonJS, the --require form alone is enough
- 2. ESM on Node.js earlier than v20.6 - add --loader dd-trace/loader-hook.mjs
- 3. ESM on Node.js v20.6 or later - add --import dd-trace/register.js, or combine everything into the single dd-trace/initialize.mjs
- 4. The framework owns the entry point, as with next start - write it in NODE_OPTIONS or in the start script in package.json
Next.js and Nest.js start not with node app.js but with something like next start, using a file bundled with the framework as the entry point. In that case, loading the SDK early in the app's code has no effect, because the framework may already have loaded the modules you want to instrument. The official documentation shows two remedies. One is to put NODE_OPTIONS='--require dd-trace/init' before the startup command. The other is to rewrite the start script in package.json as node --require dd-trace/init ./node_modules/.bin/next start. Either works, but they differ in that NODE_OPTIONS also applies to child Node.js processes (source: Node.js Compatibility Requirements).
What Disappears When You Bundle into One File with esbuild or Webpack?
The require() calls that dd-trace intercepts are replaced with the bundler's own code when the bundler combines the app into one file. Calls to built-in modules such as fs and http remain as they are, so disk reads and writes and outbound HTTP requests are recorded, but calls into libraries installed from npm can no longer be intercepted. The examples given in the official documentation are that express route information and the queries run with mysql go missing (as of October 2026; source: Tracing Node.js Applications). There is a workaround of moving every library you want to instrument outside the bundle (external), but it adds extra files and weakens the point of bundling, so Datadog recommends the bundler plugins.
| Bundler | What to use | Minimum requirements |
|---|---|---|
| esbuild | Pass the dd-trace/esbuild plugin to plugins | dd-trace@3 or later, Node.js v16.17 or v18.7 or later |
| Webpack | The dd-trace/webpack plugin (experimental support) | dd-trace@5.94.0 or later |
| Next.js | Add the @datadog native modules to externals in the webpack configuration in next.config.js | next@10.2 or later in the compatibility table |
With any bundler, put the C++ native modules the SDK uses (mostly with the .node extension) in external, and ship node_modules/ along with the bundle separately. These are in packages whose names start with @datadog. The esbuild example lists @datadog/native-metrics for runtime metrics, @datadog/pprof for profiling, @datadog/native-appsec for security features, and others. You do not need to ship the entire node_modules/; it is enough to check the required versions with npm ls, install them into a temporary directory, and copy only its contents. For Next.js, the destination is usually .next/standalone. Note that Dynamic Instrumentation cannot be used in a bundled app.
The name in package.json Becomes the Default service
Preparing the receiving Datadog Agent, and the approach to the environment variables that determine the destination host and port and log correlation, overlap with the Python SDK, so they are left to the section on where data is sent in the Python setup article. The defaults specific to the Node.js SDK that you should keep in mind are the following two.
| Environment variable | Default | What happens if you omit it |
|---|---|---|
| DD_SERVICE | The name in package.json | The package name becomes the service name as is |
| DD_RUNTIME_METRICS_ENABLED | false | Runtime metrics such as garbage collection and memory usage do not arrive |
Of the three tags in unified service tagging, only service is filled in even if you pass nothing. If you go to production without deciding on a name, the package name from development appears as is in the service list. Runtime metrics, conversely, send nothing until you pass true. If you choose the --require dd-trace/init or NODE_OPTIONS form, you pass all of these values through environment variables.
What to Check in the Compatibility Table Is the Minimum for Node.js, the OS, and Libraries
As of October 2026, the minimum Node.js version supported by the Node.js SDK is v18. For v16, which has reached end of life, npm install dd-trace@latest-node16 with the distribution tag latest-node16 is offered (source: Tracing Node.js Applications). Each major version of dd-trace enters maintenance when the next major version is released and receives security and bug fixes, where feasible, for one year from that release date. When a Node.js LTS line reaches end of life, support for that Node.js is dropped in the next major version of dd-trace. No special versions are released for odd-numbered versions (source: Node.js Compatibility Requirements).
| Category | Library | Minimum and notes |
|---|---|---|
| Web framework | express / fastify / koa / hono | express 4 or later, fastify 1 or later, koa 2 or later, hono 4 or later |
| Web framework | next | 10.2 or later |
| Built-in module | http2 | Partial support for the client only (the server is not covered) |
| Data store | pg / mysql2 | pg 4 or later, mysql2 1 or later |
| Data store | prisma | Partial support from 6.1.0 (integration with Database Monitoring is not covered) |
The compatibility table says that it is likely to work on operating systems not in the table, but some features will be missing. For Next.js, the compatibility table itself notes that it is not unusual for a patch version update to break compatibility with dd-trace, and that the follow-up fix can take a few days. As one way to proceed, in apps that use Next.js, keeping framework updates and SDK updates on separate days makes it easier to isolate which one is the cause when traces go missing.
With Lambda and Single Step Instrumentation, Do Not Add dd-trace as a Dependency
In serverless environments and with Single Step Instrumentation, the SDK is already present, so you do not add dd-trace to the app's dependencies. Only when trying it out locally do you install it as a development dependency with npm install dd-trace -D. If you write custom instrumentation there, import dd-trace-api instead of dd-trace (as of October 2026; source: Tracing Node.js Applications). How to add it to Lambda functions is left to the Lambda Extension setup article, and the form in which the operations side injects the SDK to automatic instrumentation. The compatibility table also lists the requirements for the feature that automatically redacts the inline authentication arguments of Redis 6.0 and later from traces. They are Trace Agent 7.76.1 or later and Lambda Extension v28.0.0 or later (source: Node.js Compatibility Requirements).
Which Line of the App's Startup Does Loading dd-trace Go Into?
Where people get stuck in a Node.js setup is less the SDK's configuration values than which command the app starts with. Whether node app.js is called directly, next start runs behind npm start, or the container's startup command calls another script changes where you write --require.
- 1. Write out the production startup command all the way to the end - whether it calls node directly or goes through npm or a framework CLI
- 2. Decide what to add - --loader or --import for ESM, the plugin and external if you bundle, and DD_SERVICE / DD_ENV / DD_VERSION for the names
- 3. Start with DD_TRACE_DEBUG=true - check the SDK's detailed logs for send failures
Once traces arrive, check in the APM screens whether the spans of web requests such as express carry a route, and whether database spans are connected to their parent request. If the route is missing, the loading order is the first thing to suspect; if only the spans of libraries installed from npm are missing, it is the bundling configuration. Open the package.json in your repository and the container's startup command today, and try writing out which line dd-trace is designed to be loaded on.