azure-sdk-for-js
azure-sdk-for-js copied to clipboard
ESM import of @azure/monitor-opentelemetry fails
- Package Name: @azure/monitor-opentelemetry
- Package Version: 1.2.0
- Operating system: Windows
- [x] nodejs
- version: 20.10
- [ ] browser
- name/version: N/A
- [x] typescript
- version: 5.3.3
- Is the bug related to documentation in
- [ ] README.md
- [ ] source code documentation
- [ ] SDK API docs on https://docs.microsoft.com
Describe the bug In a project using ESM ("type": "module") it seems like @azure/monitor-opentelemetry incorrectly references the underlying @opentelemetry/resource library, giving the following error:
var resources = require('@opentelemetry/resources');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\...\node_modules\@opentelemetry\resources\build\src\index.js from C:\Users\...\node_modules\@azure\monitor-opentelemetry\dist\index.js not supported.
C:\Users\...\node_modules\@opentelemetry\resources\build\src\index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename C:\Users\...\node_modules\@opentelemetry\resources\build\src\index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\Users\...\node_modules\@opentelemetry\resources\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at Object.<anonymous> (C:\Users\...\node_modules\@azure\monitor-opentelemetry\dist\index.js:8:17) {
code: 'ERR_REQUIRE_ESM'
}
To Reproduce Steps to reproduce the behavior:
- Create a simple node project with typescript and @azure/monitor-opentelemetry
- Import the useAzureMonitor function
import { useAzureMonitor } from '@azure/monitor-opentelemetry';
useAzureMonitor();
Expected behavior It works and starts sending data to the insights resource indicated in the APPLICATIONINSIGHTS_CONNECTION_STRING env var
Additional context
Here's my tsconfig.json file if that helps
{
"compilerOptions": {
"module": "NodeNext",
"target": "ES2020",
"lib": ["ES2020"],
"skipLibCheck": true,
"outDir": "dist",
// "esModuleInterop": true,
// "allowJs": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"isolatedModules": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
},
"include": ["src"],
}
@alexgagnon I've recreated an application using the reproduction steps detailed in your issue (including the tsconfig.json) and I don't appear to have any errors thrown. Could you give this a try with the latest version of the @azure/monitor-opentelemetry package?
Hi @alexgagnon. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Hi @alexgagnon, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!