dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

[FEATURE]: Improve support for running Typescript natively through Node.js

Open przbab opened this issue 11 months ago • 2 comments

Package Name

No response

Package Version(s)

No response

Describe the feature you'd like

I would like to request a way to use Node.js native Typescript (currently hidden behind experimental flag) with all of the expected data in APM, traces, etc.

Is your feature request related to a problem?

We tried updating our app with native Node.js type stripping running it like

node --experimental-strip-types src/index.ts

or

node --experimental-transform-types src/index.ts

App works fine but we lost some data in monitoring. App is using Koa library and after the switch we lost operation:koa.request and instead got operation:web.request in APM and traces were incomplete. I confirmed similar behaviour with another team using Hapi library. Tested on both Node v22.14.0 and v23.10.0.

My suspicion is that we need to add --import dd-trace/register.js part to the command as described for ESM in https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/#esm-applications-only-import-the-loader but that breaks the app completely on first Typescript code it encounters with error like this:

Error: 'import-in-the-middle' failed to wrap ...

Describe alternatives you've considered

I tried using import-in-the-middle directly to exclude possibility of --import flag breaking anything which let me start the app but just delays the problem until execution encounters typescript file after registering import-in-the-middle/hook.mjs.

Additional context

Not sure if this is useful but minimal example I used to reproduce the issue is:

// tmp.ts
const myVariable: string = 'Hello, World!';

console.log(myVariable);

and running it with

node --import=dd-trace/register.js --experimental-strip-types ./tmp.ts

which results in error:

Error: 'import-in-the-middle' failed to wrap...

const myVariable: string = 'Hello, World!';
      ^^^^^^^^^^

SyntaxError: Missing initializer in const declaration

Connected to https://github.com/nodejs/import-in-the-middle/issues/186

przbab avatar Apr 01 '25 09:04 przbab

Did you figure out a workaround? I'm encountering something very similar

sdotson avatar Jun 04 '25 13:06 sdotson

Did you figure out a workaround? I'm encountering something very similar

Unfortunately not, I rolled back to a version not using native type stripping in projects using DataDog monitoring.

przbab avatar Jun 05 '25 11:06 przbab

We tried again running project with Node's native type stripping, we even used --experimental-transform-types, now everything seems to be in order, no data loss. I'm guessing it was fixed sometime and we didn't notice. We are running dd-trace in version 5.57.1. For us full command looks like this:

node --import dd-trace/register.js --experimental-transform-types src/index.ts

--import dd-trace/register.js is still required to have full data (which is inline with the documentation https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/#esm-applications-only-import-the-loader).

I'm closing this issue as it seems fixed to me.

przbab avatar Aug 26 '25 12:08 przbab