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

OpenTelemetry with NodeJs and Typescript #4812

Closed
g1nsu opened this issue Jul 9, 2024 · 9 comments
Closed

OpenTelemetry with NodeJs and Typescript #4812

g1nsu opened this issue Jul 9, 2024 · 9 comments
Labels
bug Something isn't working sig:javascript

Comments

@g1nsu
Copy link

g1nsu commented Jul 9, 2024

When setting up NodeJs with Typescript, the documentation suggests using the --require flag. However, I received an error ("Cannot use import statement outside of a module.") when taking this approach.

When I reviewed the NodeJs documentation for the --require flag from the link in the OpenTelemetry page, it suggested using the --import flag for ECMA modules. Once I made this change, everything started working. I must also note that I am using tsx to run the project.

Mentioning the --import flag may help some users when setting up OpenTelemetry for the first time.

@svrnm svrnm added bug Something isn't working sig:javascript labels Jul 9, 2024
@svrnm
Copy link
Member

svrnm commented Jul 9, 2024

Thanks @g1nsu

@open-telemetry/javascript-approvers can you take a look?

@trentm
Copy link
Contributor

trentm commented Jul 9, 2024

Hi @g1nsu,
Are you able to share your tsconfig.json? My guess is that your tsconfig is set so that the generated .js -- from your .ts (or .tsx?) source files -- is generating ESM code.

You are right that the TypeScript option in the current docs is assuming that instrumentation.ts is converted to CommonJS (require-using) code.

I agree that mentioning using --import could be helpful. That note should mention:

  • it is required, instead of --require, if the module to be loaded (e.g. instrumentation.js, or the transpiled instrumentation.ts) is an ESM module;
  • --import only exists on more recent versions of Node.js. It requires at least ^18.19.0 || >=20.6.0

@g1nsu
Copy link
Author

g1nsu commented Jul 9, 2024

Hi @trentm,
Yes, my tsconfig is generating ESM code. "target": "es2022",

It looks like I missed the assumption that the code was compiled to CommonJS since the TypeScript command still shows .ts files. ts-node --require ./instrumentation.ts app.ts

I think your proposed changes look great. I just thought I could save someone else a few minutes.

Thanks!

@trentm
Copy link
Contributor

trentm commented Jul 9, 2024

It looks like I missed the assumption

I'm pretty sure it isn't explicitly stated anywhere.
Thanks for opening the issue.

@svrnm
Copy link
Member

svrnm commented Jul 10, 2024

@g1nsu would you be interested to update the documentation accordingly? Could be a simple note that people might run into that and what to do in that situation

@g1nsu
Copy link
Author

g1nsu commented Jul 10, 2024

@svrnm Yes, I can certainly update the documentation. Should I just check out the Contributing documention and make a PR?

@svrnm
Copy link
Member

svrnm commented Jul 10, 2024

@svrnm Yes, I can certainly update the documentation. Should I just check out the Contributing documention and make a PR?

Thanks, that would be great! Yes, please give them a look! Especially pay attention to the CLA that we have to ask you to sign (except you have contributed to any other CNCF/LF project already). I call this out because this is a blocker for people sometimes.

@JamieDanielson
Copy link
Member

Thanks for reporting @g1nsu ! We definitely have some work to do in documenting ESM instrumentation, and have created an issue recently to help track some of the work. I'm noting this to help link for future reference as well.

@g1nsu
Copy link
Author

g1nsu commented Jul 12, 2024

After some more testing, the documentation does work as written using ts-node. Running with ts-node only works when the tsconfig has set "module": "CommonJS".

The issue I encountered has to do with tsx. The tsx package only works with the --import flag and seems to ignore the tsconfig module configuration.

If you would still like a documentation note, I can do so. But since this is a tsx specific detail, we can close this as resolved.

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

No branches or pull requests

4 participants