---
title: Node.js Lambda Tracing and Webpack Compatibility
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Serverless > Serverless Monitoring Guides > Node.js Lambda Tracing and
  Webpack Compatibility
---

# Node.js Lambda Tracing and Webpack Compatibility

## Overview{% #overview %}

Datadog's tracing libraries (`dd-trace`) are known to be not compatible with bundlers like [webpack](https://webpack.js.org) due to the use of conditional imports and other issues. While webpack cannot build `dd-trace`, your application can still use the `dd-trace` and `datadog-lambda-js` libraries provided by the prebuilt Datadog Lambda layer. Follow the instructions below.

## webpack{% #webpack %}

1. Follow the [installation instructions for Node.js](https://docs.datadoghq.com/serverless/installation/nodejs) and ensure the Datadog Lambda layer for Node.js is added to your Lambda function.

1. Remove `datadog-lambda-js` and `dd-trace` from your `package.json` and the build process.

1. Mark `datadog-lambda-js` and `dd-trace` as [externals](https://webpack.js.org/configuration/externals/). This tells the bundler to skip building them as dependencies, since they are already available in the Lambda runtime provided by the Datadog Lambda layer.

**webpack.config.js**

   ```gdscript3
   var nodeExternals = require("webpack-node-externals");
   
   module.exports = {
     // use webpack-node-externals to exclude all node dependencies.
     // You can manually set the externals too.
     externals: [nodeExternals(), "dd-trace", "datadog-lambda-js"],
   };
   ```

1. If you are using the `serverless-webpack` and have the option `includeModules` set to any value other than `false`, serverless-webpack automatically [packs external modules under node_modules](https://github.com/serverless-heaven/serverless-webpack#node-modules--externals). Therefore, you must force exclude `datadog-lambda-js` and `dd-trace`. Skip this step if you don't use `serverless-webpack` or you don't have the `includeModules` option in your serverless.yml.

**serverless.yml**

   ```
   custom:
     webpack:
       # Note: You only need the following if you already have the includeModules option configured
       includeModules:
         # ... your existing configuration for includeModules
         forceExclude:
           - dd-trace
           - datadog-lambda-js
       packagerOptions:
         scripts:
           # optional, only needed when they are included as transitive dependencies 
           - rm -rf node_modules/datadog-lambda-js node_modules/dd-trace
   ```

## esbuild{% #esbuild %}

1. Follow the [installation instructions for Node.js](https://docs.datadoghq.com/serverless/installation/nodejs) and ensure the Datadog Lambda layer for Node.js is added to your Lambda function.

1. Remove `datadog-lambda-js` and `dd-trace` from your `package.json` and the build process.

1. Mark `datadog-lambda-js` and `dd-trace` as [externals](https://esbuild.github.io/api/#external). This tells the bundler to skip building them as dependencies, since they are already available in the Lambda runtime provided by the Datadog Lambda layer.

1. Follow the steps on the [Esbuild support](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/nodejs/?tab=containers#esbuild-support) page to use Datadog's Esbuild plugin. This enables instrumentation of bundled dependencies.

**esbuild.config.js (if using esbuild-config)**

   ```
   {
     "external": ["dd-trace", "datadog-lambda-js"],
   }
   ```

**serverless.yml (if using serverless-esbuild)**

   ```mysql
   custom:
     esbuild:
       exclude: ["dd-trace", "datadog-lambda-js", "aws-sdk"] # aws-sdk is needed because it is the default value for `exclude`
   ```

## Further Reading{% #further-reading %}

- [Instrumenting Node.js Applications](https://docs.datadoghq.com/serverless/installation/nodejs)
