Consensys / armlet

a MythX API client wrapper
MIT License
16 stars 7 forks source link

TypeError: URL is not a constructor :( #105

Closed muellerberndt closed 5 years ago

muellerberndt commented 5 years ago

After #104, armlet still errors in the browser at the same line as before:

module.exports.defaultApiUrl = new URL(defaultApiUrl)

This time the error is:

Uncaught TypeError: URL is not a constructor

Which is surprising because URL is supposed to be a built-in class and I'm convinced I had it working exactly like that. Tested in Safari and Chrome.

muellerberndt commented 5 years ago

Just found this StackOverflow answer. Maybe the URL variable is overwritten somewhere?

I also tried this code in the browser (it works):

<html>
<head>

<script>

var URL
URL = URL || require('url').URL

var url = new URL("https://www.google.com");

console.log(url);

</script>
muellerberndt commented 5 years ago

Also, for reference, this is the context where I'm getting the exception:

https://github.com/aquiladev/remix-ide/tree/mythx

muellerberndt commented 5 years ago

@nbanmp is this code necessary?

var URL
URL = URL || require('url').URL

This is what seems to break the remix-ide, it works when removed.

birdofpreyru commented 5 years ago

@b-mueller my guess is that you need:

var URL
URL = (typeof window !== 'undefined' && window.URL)
  ? window.URL : require('url').URL
muellerberndt commented 5 years ago

Yep, that works.

vdenisov-pro commented 4 years ago

Here is an example of this error occurring.

1) In the first file Twilio.js (name not important) an attempt is made to call the URL class from the url library ( var url = require ('url'); ).

2) The second file url.d.ts (which stores types for the url library) has a description for the URL class. So when you write new url.Url linter does not swear, because the class being called is described in the typing. This usually means that the library has similar functionality.

... but not this time ...

3) In the third file url.js (where the basic logic for the url library is described) the URL class is missing. And this is why the error occurs. As a result, the url.URL is not a constructor or function or anything else. It's just undefined.

P.S.:

twilio-URL-problem

javiermendozain commented 3 years ago

This work for me:

Change this: var URL = window.URL;

By this:

function URL(url, base) {
  this.url = url;
  this.base = base;
  return window.URL;
}

on file node_modules/recordrtc/RecordRTC.js

I did with that script on package "postinstall": "cp patch/RecordRTC.js node_modules/recordrtc/RecordRTC.js"

hiepxanh commented 1 year ago

tested with angular 14 SSR, you should lazy load it to prevent it leak to override global namespace URL of nodejs (mention here https://github.com/muaz-khan/RecordRTC/issues/666)

dont have to hack in node_modules

        let RecordRTC;
        let moduleRecord;
        await this.ngZone.run(async () => {
          // https://stackoverflow.com/a/58859327/5748537
          moduleRecord = await import('recordrtc');
          RecordRTC = (moduleRecord as any).default;
        });

        this.record = new RecordRTC(this.stream, {
          mimeType: 'audio/wav',
          type: 'audio',
          numberOfAudioChannels: 1,
          disableLogs: true,
          desiredSampRate: 12000,
          recorderType: RecordRTC.StereoAudioRecorder,
        }); 

do you love me?