Apollo-link-rest: ReferenceError: Headers is not defined

Created on 3 Jan 2018  ·  10Comments  ·  Source: apollographql/apollo-link-rest

Tried the basic code out in node 8 using the readme and its bombing out.

/home/stephen/dev/gql-play/node_modules/apollo-link-rest/lib/bundle.umd.js:110
    if (headers instanceof Headers) {
                           ^

ReferenceError: Headers is not defined
    at normalizeHeaders (/home/stephen/dev/gql-play/node_modules/apollo-link-rest/lib/bundle.umd.js:110:28)
    at new RestLink (/home/stephen/dev/gql-play/node_modules/apollo-link-rest/lib/bundle.umd.js:259:25)
    at Object.<anonymous> (/home/stephen/dev/gql-play/index.js:7:14)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)

Attached source code below
gql-play.zip

➜  gql-play node -v
v8.9.3

Most helpful comment

I was able to work around it by putting the headers on the global object.

const fetch = require('node-fetch');
global.Headers = fetch.Headers;

All 10 comments

I was able to work around it by putting the headers on the global object.

const fetch = require('node-fetch');
global.Headers = fetch.Headers;

I have tried to reproduce it here : https://codesandbox.io/s/0x4wx2or0v without any luck. Can you try to ?

Using your example, I have the same problem, tried also with node 6.1, same result.

I think I’m running with node9? I’ll need to downgrade my node and see if that reproduces. I’m having other node problems.

did they add Headers to global in node 9? https://runkit.com/trashhalo/5a4d5779ceaa39001278edbe 9.3.0 seems to still error out

@trashhalo I'm using v9.3.0 -- are you still exhibiting this problem?

Hey @trashhalo -- I just ran your test-app, and you're right, when trying to run link-rest directly as a node-script I get the same missing Headers problem.

I was able to get past that problem by doing:

global.Headers = global.Headers || require("fetch-headers");

However, obviously, that's polyfilling the problem. -- Unfortunately when I do that, I get an error with ReferenceError: gql is not defined

I'm not entirely certain we expect link-rest to work when called in a pure node environment? Thoughts?

Oh okay. I don't mind polyfilling the issue assuming it works.

Hi All,

node v8.94
OS X 10.11.6
Next.JS v 5.0.1-canary.4

I installed the Advanced Example, updated all dependencies to latest and all was well.

I then copied src/SearchShow.js and supporting <ApolloProvider> (and deps) into a new project, running under Next.JS and the problem appeared.

I applied @fbartho's polyfill and the problem no longer appeared. Also, I do not see the ReferenceError: gql is not defined error, as reported by @fbartho.

In hopes it will be helpful, that project is available here.

I have same issue with node v9.7.1.

I'm adding to the documentation that you may need to polyfill Headers for your target runtime environment, additionally, further enhancements are being discussed in #93, so I'm going to close this ticket.

Was this page helpful?
0 / 5 - 0 ratings