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

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. #22

Open
gustawdaniel opened this issue Mar 27, 2023 · 1 comment · May be fixed by #24

Comments

@gustawdaniel
Copy link
Contributor

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 added a commit to gustawdaniel/human-object-diff that referenced this issue Mar 28, 2023
@gustawdaniel gustawdaniel linked a pull request Mar 28, 2023 that will close this issue
@spence-s
Copy link
Owner

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.

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

Successfully merging a pull request may close this issue.

2 participants