Node.js Lambda Tracing and Webpack Compatibility

Compatibility

Datadog’s tracing libraries (dd-trace) are known to be not compatible with webpack 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:

  1. Follow the installation instructions for Node.js and ensure the Datadog Lambda layer is added to your Lambda function.

  2. Mark datadog-lambda-js and dd-trace as externals for webpack. This tells webpack to skip building them as dependencies, since they are already available in the Lambda runtime provided by the Datadog Lambda layer.

    webpack.config.js

    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"],
    };
    
  3. Remove datadog-lambda-js and dd-trace from your package.json and the build process.

  4. If you are using the serverless-webpack or serverless-esbuild plugin for the Serverless Framework, exclude datadog-lambda-js and dd-trace from your serverless.yml.

    serverless.yml

    custom:
      webpack: # for webpack
        includeModules:
          forceExclude:
            - dd-trace
            - datadog-lambda-js
      esbuild: # for esbuild
        exclude: ["dd-trace", "datadog-lambda-js"]
    

    Note: This exclusion may not be sufficient if you have any transitive dependencies on datadog-lambda-js or dd-trace. In these cases, forceExclude does not avoid the inclusion of one of these libraries. If this is your case, you can try to remove them manually using something like the following:

    custom:
      webpack:
        packagerOptions:
          scripts:
            - rm -rf node_modules/datadog-lambda-js node_modules/dd-trace
    

Further Reading

Additional helpful documentation, links, and articles: