Got: Cannot read property 'startsWith' of undefined

Created on 16 Dec 2019  Â·  11Comments  Â·  Source: sindresorhus/got

Describe the bug

  • Node.js version: v10.15.3
  • OS & version: macOS 10.15.1

    • Got: version 10.0.4

When trying to make a POST request to https://www.google.com I get an error using version 10.0.4 but getting back to version 9.0.4 returns a correct response.

Actual behavior

When trying to make a POST request to https://www.google.com I get the error

{ GotError: Cannot read property 'startsWith' of undefined
    at get (/Users/cesargdm/Development/Sites/fucesa-cloud/node_modules/got/dist/source/request-as-event-emitter.js:200:27)
    at urlToOptions (internal/url.js:1259:28)
    at Object.request (https.js:271:15)
    at Object.request (/usr/local/lib/node_modules/serverless/node_modules/agent-base/patch-core.js:23:20)
    at get (/Users/cesargdm/Development/Sites/fucesa-cloud/node_modules/got/dist/source/request-as-event-emitter.js:197:43) code: undefined, name: 'RequestError' }

Expected behavior

Don't throw error,

Code to reproduce

got.post('https://www.google.com')

Checklist

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
bug ✭ help wanted ✭

Most helpful comment

https://github.com/nodejs/node/pull/26226 was backported to v10.x via https://github.com/nodejs/node/pull/32446. It should be available >= 10.20.0 which is unreleased as of writing this message.

All 11 comments

@cesargdm It would be helpful if you could submit a pull request with a failing tests.

I'm also having this problem. It seems related to this issue https://github.com/nodejs/node/issues/26198

const response = await got("https://www.google.com");

The hello world of got is failing with with this error, using 10.5.5 with nodejs 10.16.0 on MacOS 10.15.2:

GotError: Cannot read property 'startsWith' of undefined\n at get (/Users/anon/WebstormProjects/artha/node_modules/got/dist/source/request-as-event-emitter.js:199:27)\n at urlToOptions (internal/url.js:1259:28)\n at Object.request (https.js:279:15)\n at Object.<anonymous> (/Users/anon/WebstormProjects/artha/node_modules/agent-base/patch-core.js:23:20)\n at get (/Users/anon/WebstormProjects/artha/node_modules/got/dist/source/request-as-event-emitter.js:196:65)

@apoorvans It's a bug in Node.js 10. The issue has been fixed >= Node.js 12.

@apoorvans It's a bug in Node.js 10. The issue has been fixed >= Node.js 12.

Thanks much @szmarczak, will try node 12.

https://github.com/nodejs/node/pull/26226 was backported to v10.x via https://github.com/nodejs/node/pull/32446. It should be available >= 10.20.0 which is unreleased as of writing this message.

For all those coming here because of this somewhat ephemeral issue with got
The culprit is not a node version, but agent-base implementation visible in original stacktrace as well. It incorrectly clones URL which results in a later fail in internal node request code. The fix in Nodejs referenced above only fixes problem as in it provides a failsafe validation for URL - the request will still work incorrectly since url.hostname will not be present.

The real issue is that even though agent-base has been updated to fix the problem, proxy-agent (which uses it) was not - see https://github.com/TooTallNate/node-proxy-agent/issues/51. Solution for my case was to use npm-force-resolutions to override transitive dependency over agent-base in my project.

@artem-karpenko Can you please explain what exactly have you done to fix this?

It seems like Google Cloud Function's nodejs10 runtime updated it's version and now we can't deploy any service using got as they all fail, which is a pretty big issue for us. I don't understand this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carvallegro picture carvallegro  Â·  4Comments

dominusmars picture dominusmars  Â·  3Comments

framerate picture framerate  Â·  4Comments

alanzhaonys picture alanzhaonys  Â·  4Comments

jamestalmage picture jamestalmage  Â·  3Comments