Skip to content

Module parse failed - webpack #652

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

Closed
bshyong opened this issue Oct 25, 2022 · 12 comments
Closed

Module parse failed - webpack #652

bshyong opened this issue Oct 25, 2022 · 12 comments
Labels
bug Something isn't working

Comments

@bshyong
Copy link

bshyong commented Oct 25, 2022

Describe the bug

I cannot get the latest module to work, is there something I need to do for this to work with webpack?

react | ERROR in ./node_modules/@web3modal/react/dist/index.js 125:25
react | Module parse failed: Unexpected token (125:25)
react | File was processed with these loaders:
react |  * ./node_modules/babel-loader/lib/index.js
react | You may need an additional loader to handle the result of these loaders.
react | |         h(!1), s(!0);
react | |         try {
react | >           v = await r(O ?? t), P.isNull(v) || (g(v), H(void 0), s(!1));
react | |         } catch (Y) {
react | |           Y instanceof Error ? H(Y) : H(new Error("Unknown error")), g(void 0), s(!1);
react | ℹ 「wdm」: Failed to compile.

SDK Version

  • Version 2.0.0-alpha.8

To Reproduce
Steps to reproduce the behavior:

  1. Run sample project with defaults
@bshyong bshyong added the bug Something isn't working label Oct 25, 2022
@xzilja
Copy link
Contributor

xzilja commented Oct 25, 2022

You need to configure webpack / babel to work with ES2020 standard (this is what we are targeting). I believe webpack is complaining about that optional chaining operator ??

@xzilja xzilja closed this as completed Oct 25, 2022
@paintoshi
Copy link

@bshyong Were you able to solve it? I'm running into the same issue and I already have this in my tsconfig.json so not sure what else to change.

 "compilerOptions": {
    "target": "es2020",

@xzilja
Copy link
Contributor

xzilja commented Nov 9, 2022

@paintoshi Depending on which tools you use, adding this won't be enough. Usually it is within webpack / babel etc. where you need to add appropriate loaders or plugins to target modern js.

Latest versions of more modern tools like vitest / rollup etc. should work by default, I also think latest babel version also works with es2020.

@paintoshi
Copy link

@iljadaderko Ok thank you for quick reply. The app was created with Create React App, and I have an old react-scripts package, which in turn uses an old babel-loader. Updating it created plenty of other issues in other packages, so I need to look into this more. Probably have to update a lot to get this working.

@Edison4mobile
Copy link

Any update with this?

@xzilja
Copy link
Contributor

xzilja commented Dec 6, 2022

@Edison4mobile TL;DR fix was to update babel to latest version that supports ES2020

@paintoshi
Copy link

@Edison4mobile

I can confirm that it works with the latest yarn create react-app wallet-demo --template typescript
I just had to add this line to a .env.local file GENERATE_SOURCEMAP=false
Or it creates hundreds of source file errors. It's something with the latest react-scripts 5. v4 does not have that problem but not sure if web3modal works with that. More info: mswjs/msw#1030

The only problem for me is actually updating React in our live platform. It's tremendously complex with all our dependencies. I would not be sad if web3modal worked with lower versions..

@xzilja
Copy link
Contributor

xzilja commented Dec 22, 2022

@paintoshi In theory you should be able to get it working with v4 of create react app if you add babel loaders for some es2020 syntax (which works in v5 as it uses latest babel version I believe), from the top of my head:

  1. Optional chaining
  2. Nullish coalescing

@paintoshi
Copy link

@0xasimetriq I wonder if that's possible without ejecting the whole thing, which we probably won't do since it's a one-way operation.

@paintoshi
Copy link

paintoshi commented Jan 24, 2023

@0xasimetriq Dug some more into this. Everything works while running [email protected] but that may not be an option for some since it requires React v18 which creates a lot of other problems with some dependent libraries (depending on the app).

But the problem above can be avoided without upgrading anything and without ejecting the app which cannot be reverted.

  1. Install https://www.npmjs.com/package/react-app-rewired
  2. Install https://www.npmjs.com/package/react-app-rewire-babel-loader
  3. Add the following to your config-overrides.js
const path = require("path");
const fs = require("fs");
 
const rewireBabelLoader = require("react-app-rewire-babel-loader");
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);

module.exports = function override(config) {
  config = rewireBabelLoader.include(
    config,
    resolveApp("node_modules/wagmi"),
    resolveApp("node_modules/@web3modal"),
    resolveApp("node_modules/@wagmi"),
    resolveApp("node_modules/@walletconnect")
  );

  return config
}
  1. Change the scripts in package.json to use rewired (all except eject)
"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },

Validated running web3modal 2.0 together with old version of create-react-app and [email protected] and [email protected]

@xzilja
Copy link
Contributor

xzilja commented Jan 24, 2023

@paintoshi awesome! Ty for the update, also I was under impression that react-scripts respects babel config file even when not ejected, but I guess that is wrong (been a while since I used it).

@EperezOk
Copy link

@0xasimetriq Dug some more into this. Everything works while running [email protected] but that may not be an option for some since it requires React v18 which creates a lot of other problems with some dependent libraries (depending on the app).

But the problem above can be avoided without upgrading anything and without ejecting the app which cannot be reverted.

  1. Install https://www.npmjs.com/package/react-app-rewired
  2. Install https://www.npmjs.com/package/react-app-rewire-babel-loader
  3. Add the following to your config-overrides.js
const path = require("path");
const fs = require("fs");
 
const rewireBabelLoader = require("react-app-rewire-babel-loader");
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);

module.exports = function override(config) {
  config = rewireBabelLoader.include(
    config,
    resolveApp("node_modules/wagmi"),
    resolveApp("node_modules/@web3modal"),
    resolveApp("node_modules/@wagmi"),
    resolveApp("node_modules/@walletconnect")
  );

  return config
}
  1. Change the scripts in package.json to use rewired (all except eject)
"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
  },

Validated running web3modal 2.0 together with old version of create-react-app and [email protected] and [email protected]

Thanks! I just had to remove the resolveApp("node_modules/@walletconnect") line inside config-overrides.js, otherwise I was getting a compile error from that dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants