question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pedrodurekcommented, Apr 5, 2021

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 returning undefined.

import defaultBackend from "i18next-http-backend"; // Returns undefined

However, if you rely on the es5 syntax (require), it works! That’s because it’s pointing to ./cjs/index.js now.

const defaultWithRequire = require("i18next-http-backend"); // Returns the proper object

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.

1reaction
adraicommented, Apr 4, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found