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

Reference of require in esm build of base sdk packages #2464

Closed
legendecas opened this issue Sep 9, 2021 · 12 comments
Closed

Reference of require in esm build of base sdk packages #2464

legendecas opened this issue Sep 9, 2021 · 12 comments
Labels
bug Something isn't working

Comments

@legendecas
Copy link
Member

legendecas commented Sep 9, 2021

Please answer these questions before submitting a bug report.

What version of OpenTelemetry are you using?

0.25.0

Please provide the code you used to setup the OpenTelemetry SDK

  • index.js
import { MeterProvider, ConsoleMetricExporter } from '@opentelemetry/sdk-metrics-base';

const meterProvider = new MeterProvider({
  exporter: new ConsoleMetricExporter(),
});

// Initialize the Meter to capture measurements in various ways.
const meter = meterProvider.getMeter('test');

const counter = meter.createCounter('metric_name', {
  description: 'Example of a counter'
});

// Create a BoundInstrument associated with specified label values.
const boundCounter = counter.bind({});
boundCounter.add(10);
  • index.html
<!DOCTYPE html>
<html lang="en">
<head></head>
<body></body>
<script type="module" src="./index.js"></script>
</html>

What did you do?

Build the file with vite or rollup in esm mode (commonjs is not enabled) and access in browser.

> vite

What did you expect to see?

The script ran without exceptions.

What did you see instead?

Uncaught ReferenceError: require is not defined

Additional context

@legendecas legendecas added the bug Something isn't working label Sep 9, 2021
@legendecas
Copy link
Member Author

Typically, for packages like lodash.merge that use the pattern module.exports = ... should be able to work with typescript option esModuleInterop enabled.

With that option enabled, we can import pkg from 'pkg' from packages either using the pattern module.exports = ... or export default ....

Notably, Node.js built-in modules already adopt the usage pattern import fs from 'fs' in esm mode (https://nodejs.org/api/esm.html#esm_builtin_modules). That is to say, when we handwriting mjs files we don't have to write import * fs from 'fs' like what we are doing now (https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-core/test/baggage/W3CBaggagePropagator.test.ts#L25). And for npm packages, the Node.js interprets module.exports = ... as the key default in ModuleNamespace object.

import * as namespace from 'cjs'

console.log(namespace);
// [Module] { default: <module.exports> }

See https://nodejs.org/api/esm.html#esm_commonjs_namespaces for more details

So that should match the intuitions on esm interoperation with cjs.

However, with typescript, we still need the option esModuleInterop enabled to make that work. AFAICT, the downside of that options is that when the compile target is the default target 'cjs' in the OTEL project, additional helper codes are generated to make the generated code behave like what ES spec defined.

@meteorlxy
Copy link

As a workaround, you can enable build.commonjsOptions.transformMixedEsModules to make it work in vite now.

import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    commonjsOptions: {
      transformMixedEsModules: true,
    },
  },
});

@legendecas
Copy link
Member Author

@meteorlxy Thanks for the suggestion. However, the problem in this issue is that the esm builds officially published can not be imported with Node.js built-in ECMAScript modules support too, that is, the ESM build product is not a valid esm build product.

@meteorlxy
Copy link

meteorlxy commented Sep 14, 2021

Yes, I also noticed this problem yesterday. However, I don't think it's a good idea to enable esModuleInterop, because that option is kind of "contagious". It might require projects who depend on opentelemetry to enable esModuleInterop, too 🤔

I'd prefer to replace lodash.merge with lodash-es or some other esm-friendly alternatives.

@legendecas
Copy link
Member Author

legendecas commented Sep 14, 2021

@meteorlxy I don't quite get a point of the "contagious" problem. As the option is just about the import behavior in enabled files, but not export behavior. Would you mind sharing any links about the "contagious" problem so that we can reach the same understanding of the options?

@dyladan
Copy link
Member

dyladan commented Sep 14, 2021

the ESM build product is not a valid esm build product.

Yes, I also noticed this problem yesterday. However, I don't think it's a good idea to enable esModuleInterop, because that option is kind of "contagious". It might require projects who depend on opentelemetry to enable esModuleInterop, too 🤔

I'd prefer to replace lodash.merge with lodash-es or some other esm-friendly alternatives.

Do you have an alternative suggestion if not esmoduleinterop?

@meteorlxy I don't quite get a point of the "contagious" problem. As the option is just about the import behavior in enabled files, but not export behavior. Would you mind sharing any links about the "contagious" problem so that we can reach the same understanding of the options?

@meteorlxy provided an example in #2467. Here it is: https://github.com/meteorlxy/esmoduleinterop-issue

I would definitely prefer not to require our customers to enable es module interop if they don't want to, but providing a broken ESM build is also not acceptable. Is lodash.merge the only broken dependency? It is only used for configuration merging so it should be simple to either find an alternative or write it ourselves.

@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@gcampbell-mythical
Copy link

I'm also using vite and seeing this issue. Neither esModuleInterop nor transformMixedEsModules fixes the issue.

@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Jan 31, 2022
@gcampbell-mythical
Copy link

This is still an issue.

@dyladan dyladan added up-for-grabs Good for taking. Extra help will be provided by maintainers and removed stale labels Jan 31, 2022
@github-actions
Copy link

github-actions bot commented Apr 4, 2022

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Apr 4, 2022
@legendecas
Copy link
Member Author

The fix should have been released in v1.1.0.

@legendecas legendecas removed up-for-grabs Good for taking. Extra help will be provided by maintainers stale labels Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants