Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

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

Open
sdavids opened this issue Mar 17, 2020 · 2 comments

Comments

@sdavids
Copy link

sdavids commented Mar 17, 2020

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
Copy link
Author

sdavids commented Mar 17, 2020

@FedeBev
Copy link

FedeBev commented Nov 22, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants