stacktracejs / error-stack-parser

Extract meaning from JS Errors
https://www.stacktracejs.com/#!/docs/error-stack-parser
MIT License
450 stars 52 forks source link

2.1.0 - incorrect TypeScript exports resulting in TypeError #75

Closed jan-molak closed 2 years ago

jan-molak commented 2 years ago

Hi @eriwen and many thanks for your work on error-stack-parser!

It looks like v2.1.0 has introduced a bug in TypeScript exports, preventing consumers of error-stack-parser from using the library in the intended way.

If you have a look at line 18 in error-stack-parser.d.ts in this diff:

Screenshot 2022-06-06 at 00 06 20

Type definitions changed in v2.1.0 suggest one should import ErrorStackParser using the default export as follows:

import ErrorStackParser from 'error-stack-parser';

ErrorStackParser.parse(...) // TypeError in 2.1.0

which is different from v2.0.7, where we'd import the module like this:

import * as ErrorStackParser from 'error-stack-parser';

ErrorStackParser.parse(...)

However, the change doesn't seem to have been applied to the associated JavaScript code, which still uses the more traditional module.exports = ErrorStackParser export.

This means that importing the default export, as per the type definitions, results in undefined and a TypeError:

TypeError: Cannot read properties of undefined (reading 'parse')

Your Environment

Possible Workaround

Ignore the type definitions and use a var require instead:

const ErrorStackParser = require('error-stack-parser');

Possible Solution

If possible, I'd suggest reverting the change to error-stack-parser.d.ts:

- export default ErrorStackParser;
+ export = ErrorStackParser;

Or, if you prefer to use default exports, the associated JavaScript code would need to change to use

+ module.exports.default = ErrorStackParser
- module.exports = ErrorStackParser

If using the default export, I'd also suggest bumping the major version number, since this change is not backwards compatible.

eriwen commented 2 years ago

Hi @jan-molak. Thanks for the report.

What I'm going to do here is revert the change (done in afc0a4c34791ce7fd535589cf796c2714cfc3462) and use export = ErrorStackParser; for v2.1.1 and then transition to export default ... (updating the JS too) for 3.0.0.

The motivation for this change was to fix https://github.com/stacktracejs/error-stack-parser/issues/45 — I'd prefer to support a single way to invoke ErrorStackParser.parse() for maximum compatibility.

Does this work for you?

jan-molak commented 2 years ago

Sure, sounds great! Thanks for getting back to me so quickly 👍🏻

AnyGong commented 2 years ago

@eriwen https://www.npmjs.com/The 2.1.2 version can be searched, but the 2.1.2 cannot be found when the nexus is installed and synchronized. Is there a faster and better solution?

jan-molak commented 2 years ago

@eriwen error-stack-parser 2.1.2 and stackframe 1.3.4 use correct exports now. However, it looks like this line in error-stack-parser.d.ts uses a default export from stackframe:

import StackFrame from "stackframe";

This results in the following error:

error TS1259: Module '"[redacted]/node_modules/stackframe/stackframe"' can only be default-imported using the 'esModuleInterop' flag

I'd suggest making the following change to error-stack-parser.d.ts:

- import StackFrame from "stackframe";
+ import StackFrame = require("stackframe");
eriwen commented 2 years ago

Ah, sorry about that @jan-molak. error-stack-parser v2.1.3 fixes both of these issues.

jan-molak commented 2 years ago

Hey @eriwen - apologies for being a pain, but it seems like v2.1.3 has fixed the issue with the stackframe, but re-introduced the issue with the default export.

in v2.1.3, error-stack-parser.d.ts:

import StackFrame = require("stackframe");  // this is fine now

declare namespace ErrorStackParser {
    export type {StackFrame};
    /**
     * Given an Error object, extract the most information from it.
     *
     * @param {Error} error object
     * @return {Array} of StackFrames
     */
    export function parse(error: Error): StackFrame[];
}

- export default ErrorStackParser;
+ export = ErrorStackParser