opentracing/opentracing-javascript

`__esModule` is `true` while no default export provided, breaks interoperability

sdavids opened this issue · 2 comments

opentracing-javascript does not work with NodeJS in experimental-modules mode when webpack is involved.

test.js

import opentracing from 'opentracing';

console.log(opentracing.globalTracer());

webpack.config.cjs

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  mode: 'production',
  target: 'node',
  externals: nodeExternals(),
  entry: path.resolve(__dirname, 'test.js'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname),
    publicPath: '/',
  },
};

package.json

{
  "name": "opentracing-example",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "webpack --config webpack.config.cjs && mv dist/bundle.js dist/bundle.cjs",
  },
  "dependencies": {
    "opentracing": "~0.14.4"
  },
  "devDependencies": {
    "webpack": "~4.42.0",
    "webpack-cli": "~3.3.11",
    "webpack-node-externals": "~1.7.2"
  }
}
$ node --experimental-modules test.js
(node:86152) ExperimentalWarning: The ESM module loader is experimental.
GlobalTracerDelegate {}
$ node bundle.cjs
...
TypeError: Cannot read property 'globalTracer' of undefined
...

The culprit in bundle.cjs is the following:

    var r = e && e.__esModule ? function () {
      return e.default
    } : function () {
      return e
    };

Due to opentracing/lib/index.js having

Object.defineProperty(exports, "__esModule", { value: true });

It tries to load the default export which does not exist.

Exact same problem here but without webpack. I'm using typescript and simple tsc for compilation