Usage with Webpack and Typescript importing cjs instead of esm
See original GitHub issueš Bug Report
A project setup with Typescript (targeting es5) and Webpack seems to be causing i18next-http-backend
to be loaded via require
which is configured to load the library from the cjs
distribution.
If I chage my import to import i18nextHttpBackend from "i18next-http-backend/esm";
it works as expected, but then TypeScript cannot find the type declaration.
To Reproduce
Sandbox: https://codesandbox.io/s/re1sn
(run npm start
if not already running)
https://re1sn.sse.codesandbox.io/
(should show something like defaultBackend is undefined
and esmBackend is function
)
Expected behavior
import i18nextHttpBackend from "i18next-http-backend";
to result in i18nextHttpBackend !== undefined
.
Your Environment
- runtime version: node v14
- i18next version: 20.1.0
- i18next-http-backend version: 1.2.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Force Webpack 4 to import cjs only and never esm with ...
How to disable importing esm modules from "module" path of libraries package.json and use only "main" entry point in webpack?
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
Finally, it's worth noting that the only way to import ESM files from a CJS module is using dynamic import() calls. This can...
Read more >How we employed the new ECMAScript Module Support in ...
Why use ECMAScript modules (import) instead of CommonJS (require)? ... The most obvious difference between ESM and CommonJS is the use of import...
Read more >CommonJS (cjs) and Modules (esm): Import compatibility
Importing ESM into CommonJS (cjs)āā Since require() is synchronous, you can't use it to import ESM modules. Instead, to import ESM into CommonJSĀ ......
Read more >ECMAScript Modules - webpack
Exporting. The export keyword allows to expose things from an ESM to other modules: Ā· Importing. The import keyword allows to get references...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @villasv, This seems to be the problem: https://github.com/i18next/i18next-http-backend/blob/master/package.json#L7-L19
Itās basically saying that if you import the library using es6 syntax (before compiling your code), itāll fallback to
./esm/index.js
, and as you specified that your target is es5, itāll not include the./esm/index.js
file, only./cjs/index.js
. Thatās why is returningundefined
.However, if you rely on the es5 syntax (
require
), it works! Thatās because itās pointing to./cjs/index.js
now.Here is an example: https://codesandbox.io/s/i18next-cjsvsesm-forked-6wypj?file=/src/App.tsx
This āexportā rule should not be considered before your code is compiled.
Iāll see what I can do in the coming days. Letās keep this issue opened for now.
Iām not a TypeScript user, but probably this is because of the target option āes5ā in the tsconfig.json ? You may want to change that to es6?