-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Typescript import example #24
Comments
I found a workaround. The problem occurs because the package is using CommonJS exports but being imported in an ES Module TypeScript context. Here's a working solution using import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const FastifyOtelInstrumentation = require('@fastify/otel')
export const fastifyOtel = new FastifyOtelInstrumentation({
servername: 'my-app',
registerOnInitialization: true,
}) Suggested ImprovementsTo make the package more TypeScript and ESM friendly, it would be helpful to:
For reference, I'm using:
|
Thanks for the report! Would you like to send a PR to address it? |
This workaround works well,but the sad thing is this will lost the type annonation for the package. The best way is to add ESM support with package. |
Related issue: fastify#24
I don't seem to have this problem. Maybe it's beginner's luck and/or my otel-fastify.ts: import FastifyOtelInstrumentation from '@fastify/otel';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
ConsoleSpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
const exporter = new ConsoleSpanExporter();
const processor = new SimpleSpanProcessor(exporter);
const tracerProvider = new NodeTracerProvider({
spanProcessors: [processor],
});
tracerProvider.register();
// If serverName is not provided, it will fallback to OTEL_SERVICE_NAME
// as per https://opentelemetry.io/docs/languages/sdk-configuration/general/.
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation();
fastifyOtelInstrumentation.setTracerProvider(tracerProvider);
export default fastifyOtelInstrumentation; tsconfig.json: {
"compilerOptions": {
"module": "NodeNext",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"noFallthroughCasesInSwitch": false,
"esModuleInterop": true,
"declarationMap": true
}
} |
Imagine the |
Yes, it depends on which module resolution strategy you use in your ts config. That said, check out https://arethetypeswrong.github.io/?p=%40fastify%2Fotel%400.5.0 to see an analysis of what is wrong with the package. These are pretty common issues with esm and module resolution node16 and they've been fixed all over the ecosystem in the last few years. You shouldn't have to apply workarounds as a consumer of this package, but for now if you use node16 module resolution you have to instantiate it like so until the types are fixed:
|
PR for adjusting the types are welcomed |
Node.js 16 is unsupported. |
The moduleResolution config value being named |
The typescript typings were exporting using the ESM style exports, but this is a CJS module, so the exports were not resolving correctly. This switches the type export to use `export = ` syntax instead which works for ESM consumers as well as CJS consumers. Fixes fastify#24 Signed-off-by: Bret Comnes <[email protected]>
It's my understanding that the type export was broken for real node ESM consumers, and the root cause of the issue being a mismatch between the module being an cjs module, but the type export using esm style exports instead of the cjs I believe this would fix this issue: https://github.com/fastify/otel/pull/30/files |
Prerequisites
Issue
I have been unable to import this module in Typescript. Can you provide an example?
This:
Yields:
And this:
Yields:
The text was updated successfully, but these errors were encountered: