sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

TypeScript w/ esModuleInterop: `import Sentry from "@sentry/{node|browser}"` is not an error, but does not work.

Open krisdages opened this issue 4 years ago • 12 comments

  • [X] Review the documentation: https://docs.sentry.io/
  • [X] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
  • [X] Use the latest release: https://github.com/getsentry/sentry-javascript/releases
  • N/A - Provide a link to the affected event from your Sentry account

Relates to PR https://github.com/getsentry/sentry-javascript/pull/3077

Package + Version

  • [X] @sentry/browser
  • [X] @sentry/node
  • [ ] raven-js
  • [ ] raven-node (raven for node)
  • [ ] other:

Version:

5.29.0

Description

Sentry must be imported using import * as Sentry instead of import Sentry in order to work. With the esModuleInterop compiler option enabled, TypeScript does not complain about import Sentry. (With the option off, TS recognizes that the module does not have a default import and forbids import Sentry)

This appears to be because the Sentry index.js module declares __esModule: true but does not actually have a value for the default export:

 // import Sentry from "@sentry/node" - transpiled
 node_1 = tslib_1.__importDefault(node_1);

 //tslib
 __importDefault = function (mod) {
        return (mod && mod.__esModule) ? mod : { "default": mod };
    };

Can the library be updated so the default import works as TypeScript thinks it does? This is only an issue when the esModuleInterop setting is on, but it's a pretty valuable setting and a dangerous mistake for the developer.

// Either 
// vv - Can this be added?
exports.default = exports;

// Or
// vv - Can this be removed?  Though it would make the `import *` less efficient. 
Object.defineProperty(exports, "__esModule", { value: true });

Thanks!

krisdages avatar Dec 07 '20 19:12 krisdages

+1 on this The typescript definition does not report an error but it's indeed broken

bodinsamuel avatar Dec 21 '20 14:12 bodinsamuel

Same goes for @sentry/serverless. Search terms:

Cannot read property 'GCPFunction' of undefined

killthekitten avatar Feb 25 '21 17:02 killthekitten

@rbisol Why is this not a bug. It basically makes this library unusable when you are working in an environment requiring interop?

fosrias avatar May 25 '21 01:05 fosrias

I’ve ran into this problem as well. Since we are using dependency injection we almost shipped a broken version of our app to production.

lo1tuma avatar Jul 12 '21 10:07 lo1tuma

FWIW, if I remove this line from the commonJS module: Object.defineProperty(exports, "__esModule", { value: true });, it works as expected

florian-milky avatar Jul 22 '21 09:07 florian-milky

any news?

imsamurai avatar Oct 13 '22 18:10 imsamurai

@imsamurai is this still an issue with the newest version of the SDK? We updated how we bundle the SDK in version 7.x.x.

lforst avatar Oct 14 '22 07:10 lforst

still a problem, no default export

incompletude avatar Dec 23 '22 23:12 incompletude

This is still a problem. Is there any fix in the works? This is incredibly dangerous especially for a module that tends to be used as part of error handling. Its very easy for this to go unnoticed until a bug causes a new exception. As an example, Sentry.captureException will generally only be called in "exceptional" circumstances and you may not have comprehensive tests for every place you call Sentry.captureException. But its a big problem for that call to fail as it breaks error reporting and causes an entirely different error in error handling code

amc6 avatar Jun 09 '23 04:06 amc6

HI @amc6,

could you share more details in how you ran into this? We do not document import Sentry from '@sentry/xxx' anywhere (because it does not work), and our TS setup should be setup properly - it is not incorrect to have no default export in ESM, as far as I can tell.

So could you share:

  • your tsconfig
  • The code that uses/references Sentry
  • Why/how you came to use a default import

mydea avatar Jun 09 '23 13:06 mydea

@mydea I did some digging around this issue a while ago and I think we have to play around with enableLegacyTypeScriptModuleInterop.

lforst avatar Jun 09 '23 18:06 lforst

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

getsantry[bot] avatar May 02 '24 07:05 getsantry[bot]

This is still an issue, the issue can stay open

Naddiseo avatar May 07 '24 18:05 Naddiseo

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

getsantry[bot] avatar Jul 05 '24 07:07 getsantry[bot]

Even though we are not working on this right now, we still shouldn't close it. The issue has 20 thumbs.

lforst avatar Jul 05 '24 07:07 lforst