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

ESM Support without custom --loader #3794

Open
johnseekins-pathccm opened this issue Nov 13, 2023 · 4 comments
Open

ESM Support without custom --loader #3794

johnseekins-pathccm opened this issue Nov 13, 2023 · 4 comments

Comments

@johnseekins-pathccm
Copy link

johnseekins-pathccm commented Nov 13, 2023

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:
#3703
#3783

@svrakitin
Copy link

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.

@johnseekins-pathccm
Copy link
Author

That is nice, but ideally we wouldn't be using CLI "hacks" to get around this issue.

@svrakitin
Copy link

I'm not sure how you could do that without ESM hooks tbh

@vanstinator
Copy link
Contributor

vanstinator commented Nov 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants