human-object-diff icon indicating copy to clipboard operation
human-object-diff copied to clipboard

after export = DiffEngine i see TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

Open gustawdaniel opened this issue 2 years ago • 1 comments

https://stackoverflow.com/questions/62273153/this-module-is-declared-with-using-export-and-can-only-be-used-with-a-defau

i have this problem only in code editor, I can lint, build and test without problems, but files with test have red flag in webstorm

Zrzut ekranu z 2023-03-28 08-46-39

I know that export = is docummented here https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require but it is not supported properly by IDE probably, on the other hand this

// CommonJS export
module.exports = DiffEngine
module.exports.default = DiffEngine

// ES module export
export default DiffEngine

works both for IDE and support all methods of imports

gustawdaniel avatar Mar 27 '23 23:03 gustawdaniel

i have this problem only in code editor, I can lint, build and test without problems, but files with test have red flag in webstorm

Sure you don't just need to restart webstorm? This seems like a webstorm problem. VSCode was not having problems for me. The TSServer is notoriously bad for not updating when new packages are installed or tsconfig is updated.

works both for IDE and support all methods of imports

No it doesn't. It does not work well for actual node.js ESM (ie... when type: 'module' in packagejson). It also does NOT emit correct types when you build.


Probably the best solution is to do what you did in the first PR and change it to an object export like

export { HumanReadable: DiffEngine }

And then we can major version bump. I think this will give us all the support required then without weird hacks to make TS happy. It's an annoying thing with TS that their support for very normal Node js patterns is crappy.

Alternatively, we can maintain the type definitions by hand in a way that supports all the modules -- see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/human-object-diff/index.d.ts

I do not like that though, I would greatly prefer that TS be able to emit correct types for the library automatically.


What we need the export to accomplish:

  1. work the same in Nodejs for both ESM/CJS typescript and CJS/ESM js targets.
  2. types are found and applied in all cases
  3. The correct types are emitted correctly during the build with no additional changes needed and solves the above 2 things.

ie in ts or js esm import {HumanReadable} from 'human-object-diff' in js cjs const {HumanReadable} = require('human-object-diff')

I think this should be possible with no hacks if we change the export to not be a default export.

spence-s avatar Mar 28 '23 12:03 spence-s