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.

Using ES version of library (node_module) is not working

See original GitHub issue

Describe the bug

Webpack throws an error when I change my code to use “es” version of libraries

Environment

image

Steps to reproduce

  1. Add @material-ui/core to your project
  2. Use any “es” variant of the code from that library. This particular code uses optional chaining in es version of the code
import TextField from "@material-ui/core/es/TextField";
  1. Webpack dev yarn start or Webpack build yarn build both fails with the following error image

The fix

  • So after digging throught the issue, I have found out that this is happening due to Optional Chaining and Nullish Coalescing not supported in the JS parser acorn v6.x.x which is what Webpack 4 uses. Issue details here
  • And on my computer, I edited the webpack config in node_modules and added the two presets to the last module rules which processes JS outside of src. image
  • This has fixed the issue

If this usecase is generic enough, Can I raise a pull request to make these changes??

If not, I can use something like react-app-rewired to edit it for our usecase. Also this change will be temporary until we move to Webpack 5.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ben-razorcommented, Dec 23, 2021

This is fixed in Create React App v5 for the modules I was having issues with.

1reaction
Aftabnackcommented, Mar 31, 2021

@Malvineous actually Babel is run for node module code as well with preset-env which uses browser list to transpile, I’d say the support is good for library code too. But acorn issue is preventing us from using Optional chaining & Nullish coalescing from library code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

was compiled against a different Node.js version using ...
Turns out my problem was user-error: make sure the version of node you are using for running is the same that you are...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
I'll conclude with three guidelines for library authors to follow: Provide a CJS version of your library; Provide a thin ESM wrapper for...
Read more >
How To Use Node.js Modules with npm and package.json
Since the default value for name will work for this tutorial, press ENTER to accept it. The next value to enter is version...
Read more >
Using ES modules in Node.js - LogRocket Blog
Learn about the state of ES modules in Node today, including concerns realted to transitioning from and interoperability with CommonJS.
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
For the last few years, Node.js has been working to support running ECMAScript ... as ES modules or CommonJS modules, and defaults to...
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