mixpanel-js icon indicating copy to clipboard operation
mixpanel-js copied to clipboard

CommonJS import error

Open dacevedo12 opened this issue 3 years ago • 3 comments

Related to https://github.com/microsoft/TypeScript/issues/45813

using the import syntax with mixpanel and typescript 4.4+ fails

import { disable, init } from "mixpanel-browser";

init("my_mixpanel_token");
disable();
       ^

TypeError: Cannot read property '_flags' of undefined

using the default import works well though, but it is discouraged by linting rules such as https://github.com/import-js/eslint-plugin-import/blob/v2.25.2/docs/rules/no-named-as-default-member.md

import mixpanel from "mixpanel-browser";

mixpanel.init("my_mixpanel_token");
mixpanel.disable();

dacevedo12 avatar Mar 23 '22 18:03 dacevedo12

Yeah, the export of the commonjs module is a Mixpanel library instance. It won't work to call individual methods without any binding to their instance.

tdumitrescu avatar Mar 23 '22 18:03 tdumitrescu

Yeah, the export of the commonjs module is a Mixpanel library instance. It won't work to call individual methods without any binding to their instance.

So would you suggest using the default import instead? Also worthy of a contribution to definitelyTyped so other TS devs don't make this mistake

dacevedo12 avatar Mar 23 '22 18:03 dacevedo12

Yes, the default export should work for you. It's the only one we document. I think it might be a pretty simple update for the lib to export a named version as well so you could do

import {mixpanel} from 'mixpanel-browser';

tdumitrescu avatar Mar 23 '22 18:03 tdumitrescu