opentracing / opentracing-javascript

OpenTracing API for Javascript (both Node and browser). 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
http://opentracing.io
Apache License 2.0
1.09k stars 108 forks source link

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

Open sdavids opened 4 years ago

sdavids commented 4 years ago

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.

sdavids commented 4 years ago

Additional info:

https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655

FedeBev commented 4 years ago

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