Open mlarcher opened 5 years ago
The package doesn't have a default export: https://github.com/opentracing/opentracing-javascript/blob/master/src/index.ts so a default import is not expected to work.
The correct import would be:
import * as opentracing from 'opentracing'
does that not work either?
I am not familiar with how --experimental-modules
work. Can it import CommonJS modules at all?
import * as opentracing from 'opentracing'
works in testing environment, but not in node environment. In node, opentracing
ends up being an object with a default
key wrapping all the exports, so everything breaks. On the contrary, that key is not present when using babel in the tests. I didn't find a way to have them both work so far.
And --experimental-modules
can import CommonJs, yes. See https://nodejs.org/api/esm.html#esm_enabling
This sounds more like a problem of general Node/Babel/TypeScript ESM support interoperability, not anything specific to OpenTracing.
Is it not possible to run Jest with --experimental-modules
? That seems like a deficiency of Jest too
Jest doesn't allow passing flags to node, and they won't support modules until they get out of experimental mode (expected for this october when node 12 goes LTS). I agree that it would be the best solution, though.
I believe it's an issue with opentracing because that's the only module that gives me this problem, all other work fine even when compiled through babel for testing purposes. The decision to pursue it or close the ticket is yours to make.
On my part, after experimenting I decided that ES modules support was not good enough yet to use them in the backend codebase, and reverted all the changes.
Do you use any other modules that are compiled with TypeScript (tsc
) and work correctly? Then we could do the same
I am running some code containing ES modules (in .mjs files) in a docker container with node:12.10.0 and the
--experimental-modules
flag. I am able toimport opentracing from 'opentracing';
and things work as expected.The issue appears when testing the code :
I am using Jest and therefore need to transpile the code through babel-jest. The setup I have works well, except for opentracing. In that case, 'opentracing' ends up being undefined, and things like
opentracing.Tags
result in an error.I'm not sure exactly what's going wrong, but it seems to be related to the default export handling :
If I comment out the
Object.defineProperty(exports, "__esModule", { value: true });
line inlib/index.js
of opentracing, things work well.They work correctly too if I add a
exports.default = exports;
in that file.Without touching the opentracing node_module, I was able to make things work in the test environment by calling
import { Tags } from 'opentracing';
, but that does not work in the production environment (i.e. no transpilation process).Please let me know if I can provide additional information to dig into the issue.