Skip to content
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

URL is not a constructor at Gaxios.validateOpts #44

Closed
dmytrobanasko opened this issue Feb 11, 2019 · 11 comments
Closed

URL is not a constructor at Gaxios.validateOpts #44

dmytrobanasko opened this issue Feb 11, 2019 · 11 comments
Assignees
Labels
released 🚨 This issue needs some love. triage me I really want to be triaged. web

Comments

@dmytrobanasko
Copy link

dmytrobanasko commented Feb 11, 2019

It looks like after migration from axios to gaxios in googleapis v37.0.0 feat: make it webpackable feature does not work properly.
Build with webpack works fine but getting an error on any request: URL is not a constructor at Gaxios.validateOpts gaxios.ts#L151
Node URL Api is incompatible with node-url in webpack's lib. Some details can be found here:
webpack/node-libs-browser#69
webpack/node-libs-browser#78

Is it supposed to be used with webpack ?

@alexander-fenster

@alexander-fenster
Copy link
Contributor

I checked the basic usage of webpacked libraries before we released v37.0.0 and it worked, but let me double check - until we're covered by some real tests, it's very possible we could break something without even noticing. Tests for browser stuff is WIP.

We are not planning to use any polyfills for url module, window.URL works just great for our purposes. It seems that gaxios has no idea (yet) that it should use window.URL, not the url module. If my understanding is correct here I'll try to fix it now.

@alexander-fenster
Copy link
Contributor

Thanks @dmytrobanasko - I was able to repro that, #45 should fix the problem.

@dmytrobanasko
Copy link
Author

That was fast =] Thank you!

@JustinBeckwith JustinBeckwith added the triage me I really want to be triaged. label Feb 12, 2019
@JustinBeckwith
Copy link
Contributor

🎉 This issue has been resolved in version 1.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@JustinBeckwith JustinBeckwith added the triage me I really want to be triaged. label Feb 12, 2019
@vc102375
Copy link

vc102375 commented Mar 5, 2019

I am still facing this issue on lambda when i run google translate api's...

2019-03-05T18:32:45.289Z 5b2767e6-8af1-47b1-9b50-86ef389b2b78 TypeError: URL is not a constructor
at Gaxios.validateOpts (/var/task/node_modules/gaxios/build/src/gaxios.js:125:27)
at Gaxios. (/var/task/node_modules/gaxios/build/src/gaxios.js:60:25)
at next (native)
at /var/task/node_modules/gaxios/build/src/gaxios.js:19:71
at __awaiter (/var/task/node_modules/gaxios/build/src/gaxios.js:15:12)
at Gaxios.request (/var/task/node_modules/gaxios/build/src/gaxios.js:59:16)
at Object. (/var/task/node_modules/gaxios/build/src/index.js:38:33)
at next (native)
at /var/task/node_modules/gaxios/build/src/index.js:19:71
at __awaiter (/var/task/node_modules/gaxios/build/src/index.js:15:12)

@alexander-fenster
Copy link
Contributor

@vc102375 How exactly are you using the code? Is it a regular Node.js application - if so, which Node.js version is used?

@alexander-fenster
Copy link
Contributor

@vc102375 A wild guess: is there any chance that you have a global variable named window defined somewhere? What does 'console.log(typeof window);` print in your code?

@vc102375
Copy link

vc102375 commented Mar 5, 2019

using Node 6.10 , i am calling google translate api's thats all I am doing not calling gaxios directly . all was fine until 14 hours ago when redeployed the api on lambda to fix a issue.

yield translate.translate(text, options)
.then(results => {
console.log(results)
const translation = results[0];
return translation;
})
.catch(err => {
console.error('ERROR:', err);
return false
});

@stephenplusplus
Copy link
Contributor

This is tracking this issue: #54

@alexander-fenster
Copy link
Contributor

@vc102375 Oh, that's bad. URL class was added in v6.13.0. The easiest way for you is switching to Node.js v8 since v6 will be EOL in less than a month.

As a quick workaround, you can add this to the very beginning of the your application:

const url = require('url'); // legacy Node6 url
const semver = require('semver');
class Node6CompatibilityURL {
  constructor(str) {
    this.url = url.parse(str);
    this.origin = this.url.protocol + '//';
    this.pathname = this.url.pathname ? this.url.pathname : '';
    this.search = this.url.search ? this.url.search : '';
    this.href = this.url.href ? this.url.href : '';
  }
}

if (semver.lt(process.version, '6.13.0')) {
  window = {};
  window.URL = Node6CompatibilityURL; // no warranty 😱 
}

Make sure you npm install semver to make it work. This code creates a global window object with window.URL that will make gaxios think it's running in browser mode, so it will use this instead of the URL class that will appear in v6.13.0. Thanks for semver check, it will keep working even after you upgrade Node.js. Please try that and let me know if it helped.

@lock
Copy link

lock bot commented Mar 12, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Mar 12, 2019
@yoshi-automation yoshi-automation added triage me I really want to be triaged. labels Apr 6, 2020
@JustinBeckwith JustinBeckwith added the triage me I really want to be triaged. label Jun 25, 2020
@JustinBeckwith JustinBeckwith self-assigned this Feb 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
released 🚨 This issue needs some love. triage me I really want to be triaged. web
Projects
None yet
Development

No branches or pull requests

6 participants