maninak / ts-xor

Compose object types containing mutually exclusive keys, using this generic Typescript utility type.
https://app.radicle.at/nodes/seed.radicle.at/rad:z3nP4yT1PE3m1PxLEzr173sZtJVnT
MIT License
106 stars 5 forks source link

Import error "The current file is a CommonJS module [...] the referenced file is an ECMAScript module [...]" #26

Closed volkerrichert closed 1 year ago

volkerrichert commented 1 year ago

Using TS 5.2 I got

src/services/common/BaseApiConfig.ts:1:15 - error TS2305: Module '"ts-xor"' has no exported member 'XOR'.

1 import type { XOR } from 'ts-xor';
                ~~~

src/services/common/BaseApiConfig.ts:1:26 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("ts-xor")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Users/volker/PhpstormProjects/bap-esim/package.json'.

1 import type { XOR } from 'ts-xor';
                           ~~~~~~~~

tsconfig compiler options look like

    "target": "es2022",
    "module": "Node16",
    "outDir": "dist",
    "sourceMap": true,
    "incremental": true,
    "declaration": true,
    "removeComments": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "typeRoots": ["./src/types", "node_modules/@types"],
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "noEmit": false,
    "pretty": true,
    "skipLibCheck": true,

    "strict": false,
    "alwaysStrict": true,
    "strictBindCallApply": true,
maninak commented 1 year ago

Thank you for reporting this, @volkerrichert !

Could you please also specify in your report the ts-xor version you're using? Have you tried using the latest v1.2.0?

Also (theoretically you shouldn't need to do it if I'm understanding things correctly), but does adding "type": "module" to your package.json file fix this import issue? If not, then in the meantime try installing a previous version that doesn't give this error to you (perhaps npm install ts-xor@~1.0?).

It would be most helpful if you could provide a GH repo or stackblitz.com to reproduce this error because I have trouble reproducing it with v1.2.0. :pray:

volkerrichert commented 1 year ago

oh, I "missed" the 1.2.0 update. I was on "1.1.0". Upgrading to 1.2.0 helps to solve the issue

maninak commented 1 year ago

Good to hear. :)