dd-trace-js
dd-trace-js copied to clipboard
ESM Support without custom `--loader`
From the README:
ESM support has been temporarily disabled starting from Node 20 as significant changes are in progress.
Is there anywhere we can track these changes or get a sense of the progress towards re-enabling ESM? Given Node docs recommend ESM rather than CommonJS, it's hard to recommend users leverage dd-trace
.
Some likely associated issues: https://github.com/DataDog/dd-trace-js/issues/3703 https://github.com/DataDog/dd-trace-js/pull/3783
I managed to make the --import
approach work on Node 20 with dd-trace v4.18.0
// esm-hooks.js
import { register } from "node:module";
import { pathToFileURL } from "node:url";
register("dd-trace/loader-hook.mjs", pathToFileURL(import.meta.url));
and then --import esm-hooks.js
instead of --loader dd-trace/loader-hook.mjs
.
That is nice, but ideally we wouldn't be using CLI "hacks" to get around this issue.
I'm not sure how you could do that without ESM hooks tbh
It's not a hack, it's the official nodejs solution for preloading modules before anything else is the --loader
flag. That's how the runtime loads dd-trace first, which then lets the module inject itself into the rest of the module loading chain where it can instrument everything and work it's magic.
Unfortunately the API hasn't been marked as stable yet, which is one of the reasons the ESM migration for the ecosystem has been such a mess. We're still stuck on CommonJS at my company due to other unrelated ESM bugs. It's amazing it's worked as well as it has thus far for folks.