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.

Importing an ESM package that uses an esModuleInterop package is not compatible in Dev mode.

See original GitHub issue

Describe the bug

I’m trying to use the onboard library, and that uses uses @gnosis.pm/safe-apps-sdk v3.0.0

@gnosis.pm/safe-apps-sdk v3.0.0 is a commonjs with the following setting:

Object.defineProperty(exports, "__esModule", { value: true });

What works:

If I import @gnosis.pm/safe-apps-sdk v3.0.0 directly it work as I would expect, in development and production.

This is how I see the transformation in dev mode:

console.log(import('@gnosis.pm/safe-apps-sdk'));
//becomes: //works fine
console.log(import('/node_modules/.vite/@gnosis_pm_safe-apps-sdk.js?v=b22e823a').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default })));
//in prod, becomes:
console.log(m(() => import("./index.b99bb17c.js").then(function(s) {return s.i}), [])) //works

in development it returns: Promise: {<value>: {default: class }} <<-- nice, this is what I expect to always get 000-correct

What does not works:

If I import the onboard library, and that library imports @gnosis.pm/safe-apps-sdk v3.0.0, it will not work in development, but will work in production.

console.log(import('@gnosis.pm/safe-apps-sdk'));
//becomes:
console.log(import('/node_modules/.vite/src-CZKXY23H.js?v=bf0e1e2e')); //it will give me problems
//in production, it becomes:
console.log(import("./index.74d6c7e3.js").then(function(s) {return s.i})) //works

In development it returns: Promise: {<value>: {default: {default: class }, Symbol: "Module" }} <<-- this is what I do not expect to get 000-wrong

The generated file in node_modules/.vite/ is also a bit different:

--- node_modules/.vite/src-CZKXY23H.js (dev mode giving me trouble)
+++ node_modules/.vite/@gnosis_pm_safe-apps-sdk.js (working dev mode)
@@ -1,6 +1,6 @@
-import {
-  __commonJS
-} from "/node_modules/.vite/chunk-SULJOONQ.js?v=39947699";
+var __commonJS = (cb, mod) => function __require() {
+  return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
+};
 
 // node_modules/@gnosis.pm/safe-apps-sdk/dist/src/communication/utils.js
 var require_utils = __commonJS({
@@ -499,4 +499,7 @@
     } });
   }
 });
-export default require_src();
+var gnosis_pm_safe_apps_sdk_default = require_src();
+export {
+  gnosis_pm_safe_apps_sdk_default as default
+};

Thanks a lot, I really appreciate the work the Vite team is doing. I hope I have provided enough information for this issue.

Reproduction

https://github.com/imsys/vite-onboard-test

System Info

System:
    OS: Linux 5.4 Manjaro Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 1.20 GB / 15.37 GB
    Container: Yes
    Shell: 5.1.12 - /bin/bash
  Binaries:
    Node: 17.3.1 - ~/.nvm/versions/node/v17.3.1/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 8.3.0 - ~/.nvm/versions/node/v17.3.1/bin/npm
  Browsers:
    Brave Browser: unknown
    Chromium: 96.0.4664.110
    Firefox: 95.0.2
  npmPackages:
    vite: ^2.7.13 => 2.7.13

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

2reactions
FunkMonkeycommented, Mar 15, 2022

I think I have a similar problem with bayesjs, which also inlcudes __esModule = true.

The file in .vite folder ends with

var bayesjs_default = require_bayes();
export {
  bayesjs_default as default
};

The generated import looks like this and because of __esModule the __vite__cjsImport2_bayesjs.default will be returned.

import __vite__cjsImport2_bayesjs from "/node_modules/.vite/bayesjs.js?v=1ee5f5bd"; const bayes = __vite__cjsImport2_bayesjs.__esModule ? __vite__cjsImport2_bayesjs.default : __vite__cjsImport2_bayesjs;

Because of this, I get the following situation, where we are already accessing the default import using the ES module syntax, but Vite additionally tries to extract another default from the returned module, which will just be undefined:

import bayes from 'bayesjs';

console.log(bayes); // -> undefined

Is this the same bug? because I can see a nested default in @imsys example, which exactly does not exist in my case…

Thanks a lot!

0reactions
ShravanSundercommented, Feb 21, 2022

i’m finding this a problem with web3Modal as well in dev mode. works with rollup

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid these issues when using new ECMAScript modules in ...
EsModuleInterop performs some changes to the typescript compiler to better handle these issues. These issues are described in the typescript ...
Read more >
Unable to import ESM .ts module in node - Stack Overflow
It seems you are looking to use ESM with Node and TS. ... Make sure to have the following attribute on your package.json...
Read more >
TypeScript - webpack
Basic Setup. First install the TypeScript compiler and loader by running: npm install --save-dev typescript ts-loader.
Read more >
Documentation - tsc CLI Options - TypeScript
Flag Type Default ‑‑allowJs boolean false ‑‑allowUmdGlobalAccess boolean false ‑‑allowUnreachableCode boolean
Read more >
File: README — AWS SDK for JavaScript
NOTE: You need to add "esModuleInterop": true to compilerOptions of your tsconfig.json . If not possible, use like import * as AWS from...
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