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.

URL is not a constructor (esm/commonjs differences)

See original GitHub issue

Getting stuck on a really weird webpack/url issue

I have the most simple code (“@ethersproject/providers”: “5.0.0-beta.164”)

  import {JsonRpcProvider} from '@ethersproject/providers';

  const RPCProvider = new JsonRpcProvider(
    `https://xxx`
  );

Which fails with (on runtime, not compile time)

TypeError: url__WEBPACK_IMPORTED_MODULE_2__.URL is not a constructor
    at geturl.js:60
    at Generator.next (<anonymous>)
    at geturl.js:8
    at new Promise (<anonymous>)
    at push.../../node_modules/@ethersproject/web/lib.esm/geturl.js.__awaiter (geturl.js:4)
    at getUrl (geturl.js:52)
    at index.js:94
    at Generator.next (<anonymous>)
    at index.js:8
    at new Promise (<anonymous>)
    at push.../../node_modules/@ethersproject/web/lib.esm/index.js.__awaiter (index.js:4)
    at index.js:91
    at fetchJson (index.js:140)
    at JsonRpcProvider.send (json-rpc-provider.js:273)
    at JsonRpcProvider.<anonymous> (json-rpc-provider.js:218)
    at Generator.throw (<anonymous>)
    at rejected (json-rpc-provider.js:6)

and the corresponding code in geturl is

 import { URL } from "url";
 const url = new URL(href);

It picks the esm compiled versions, when I check the non esm versions it actually includes URL like so

Totally unclear on why this suddenly started happening though…

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
roderikcommented, Jun 13, 2020

Happy camper here! Sorry for not helping out with the debugging

0reactions
ricmoocommented, Jun 13, 2020

No worries. All that matters is we found it. 😃

Not sure who to open a bug against upstream though; it’d be nice to help them fix this for future users…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: URL is not a constructor using WHATWG ...
I faced the same issue, then I looked into the url module and found a solution. For Node V6 use, const URL =...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
All you need to know to move from CommonJS to ECMAScript ...
We have a lot to cover, so let's jump into it! Enabling ECMAScript modules (ESM) in Node.js; Syntax; Strict by default; Browser compatibility ......
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling...
Read more >
rollup.js
Importing CommonJS · Publishing ES Modules ... file (see example) --no-esModule Do not add __esModule property --exports <mode> Specify export mode (auto, ...
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