Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommonJS import error #321

Open
dacevedo12 opened this issue Mar 23, 2022 · 3 comments
Open

CommonJS import error #321

dacevedo12 opened this issue Mar 23, 2022 · 3 comments

Comments

@dacevedo12
Copy link

Related to microsoft/TypeScript#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();
@tdumitrescu
Copy link
Member

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.

@dacevedo12
Copy link
Author

dacevedo12 commented Mar 23, 2022

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

@tdumitrescu
Copy link
Member

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';

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

No branches or pull requests

2 participants