Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling a node project with webpack + babel that imports dd-trace I get the following errors: #2465

Closed
victorjaquez opened this issue Oct 23, 2022 · 3 comments
Labels
bug Something isn't working native-addons

Comments

@victorjaquez
Copy link

Hello, I am trying to add ddtrace to a node service thats using webpack + babel. But I get the following error below. I have also followed the set up instruction in https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/nodejs/?tab=containers#typescript-and-bundlers but no luck. Any advice?

Error message

ERROR in ./node_modules/@datadog/native-appsec/vendor/linux-x64-musl/appsec.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

**Steps to reproduce**
// server.ts
import './tracer'; 

// tracer.ts
import tracer from 'dd-trace';
tracer.init(); // initialized in a different file to avoid hoisting.
export default tracer;


webpack.config.js

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { ProgressPlugin } = require('webpack');
const { name } = require('./package.json');

module.exports = {
  target: 'node',
  devtool: "inline-source-map",
  entry: './src/index.js',
  plugins: [new ProgressPlugin(), new CleanWebpackPlugin(),
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          },
        },
      },
    ],
  },
  output: {
    filename: `${name}.js`,
    path: path.resolve(__dirname, 'dist'),
  },
  externals: [
    'graphql',
    'graphql/language/visitor',
    'graphql/language/printer',
    'graphql/utilities',
  ],
};

**Environment**

* **Operation system:**
* **Node.js version: 16**
* **Tracer version: 3.5.0**
* **Agent version:**
* **Relevant library versions:**

<!-- If possible, please also provide the parts of your package.json that you're
able to share. -->
@victorjaquez victorjaquez added the bug Something isn't working label Oct 23, 2022
@tlhunter
Copy link
Member

@victorjaquez Interesting! I haven't encountered this issue before while testing dd-trace with webpack.

As a quick check, does adding @datadog/native-appsec to your list of webpack config externals fix the problem?

@bengl
Copy link
Collaborator

bengl commented Dec 15, 2022

@victorjaquez just checking in again. Did @tlhunter's suggestion work for you?

@victorjaquez
Copy link
Author

@bengl @tlhunter sorry for not updating. Solution was to use webpack-node-externals and in our case copying the node_modules to our docker image.

webpack.config.js

const nodeExternals = require('webpack-node-externals')

externals: [nodeExternals()]
Dockerfile

COPY ./dist /usr/app
COPY ./node_modules /usr/app/node_modules

Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working native-addons
Projects
None yet
Development

No branches or pull requests

4 participants