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

Library is not consumable from CJS context in TypeScript application #35

Closed
IdkMan2Usertive opened this issue Jan 18, 2023 · 4 comments
Closed
Labels

Comments

@IdkMan2Usertive
Copy link

🐛 Bug Report

Library cannot be consumed from the CJS context when using TypeScript as of TS1479 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("i18next-fs-backend")' call instead.   To convert this file to an ECMAScript module, change its file extension to '.mts' or create a local package.json file with `{ "type": "module" }`.

To Reproduce

https://codesandbox.io/s/i18next-fs-backend-issue-s4qlzc?file=/src/index.ts

Expected behavior

We should be able to import this library both from CJS context and ESM context.

Your Environment

  • runtime version: node v18
  • i18next version: ^22.4.9
  • os: Mac

Solution 🔥

In order to solve the issue you have to provide two different types declaration files, for CJS and for ESM.

I played around a bit with the package.json configuration and found out that this one is actually working, allowing me to consume this library, not matter the context (CJS / ESM).

Although "./cjs/index.d.ts" and "./esm/index.js" is the same file.

{
  "type": "module",
  "main": "./cjs/index.js",
  "module": "./esm/index.js",
  "types": {
    "require": "./cjs/index.d.ts",
    "import": "./esm/index.d.ts"
  },
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "types": {
        "require": "./cjs/index.d.ts",
        "import": "./esm/index.d.ts"
      },
      "require": "./cjs/index.js",
      "default": "./esm/index.js"
    },
    "./cjs": {
      "types": "./cjs/index.d.ts",
      "default": "./cjs/index.js"
    },
    "./esm": {
      "types": "./esm/index.d.ts",
      "default": "./esm/index.js"
    }
  },
}

Related topics 🔥

microsoft/TypeScript#49299
Swatinem/rollup-plugin-dts#210

The only one workaround for now

// @ts-ignore

😢

@adrai
Copy link
Member

adrai commented Jan 18, 2023

sorry, but this can't be the official way TypeScript wants us package maintainers to handle it...
//cc: @pedrodurek

@stale
Copy link

stale bot commented Feb 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Feb 18, 2023
@stale stale bot closed this as completed Mar 18, 2023
@e6c31d
Copy link

e6c31d commented May 18, 2023

PR to fix this issue: #36

@adrai
Copy link
Member

adrai commented May 18, 2023

should be fixed with v2.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants