React-redux-universal-hot-example: TypeError: res.writeHead is not a function

Created on 18 Feb 2016  Â·  16Comments  Â·  Source: erikras/react-redux-universal-hot-example

Hi, I've done this with your latest code:

npm run build
npm run start

Right after the _Open http://localhost:8080 in a browser to view the app._ message, I get an exception:

[0] /Users/me/projects/react-redux-universal-hot-example/src/server.js:105
[0]     res.writeHead(500, { 'content-type': 'application/json' });
[0]         ^
[0] 
[0] TypeError: res.writeHead is not a function
[0]     at ProxyServer.<anonymous> (/Users/me/projects/react-redux-universal-hot-example/src/server.js:55:9)

This is the code that fails:

// added the error handling to avoid https://github.com/nodejitsu/node-http-proxy/issues/527
proxy.on('error', (error, req, res) => {
  let json;
  if (error.code !== 'ECONNRESET') {
    console.error('proxy error', error);
  }
  if (!res.headersSent) {
    res.writeHead(500, {'content-type': 'application/json'});   // <- EXCEPTION
  }

I installed the latest http-proxy package but nothing changed.

Thanks,
Alvaro

needs info

Most helpful comment

@stalkingbutler I also run into this problem whenever I change the server side code. Instead of removing the line all together, I added a check to see if res.writeHead is a function. So far so good.

if (!res.headersSent) {
  if (typeof res.writeHead === 'function') res.writeHead(500, {'content-type': 'application/json'});
}

All 16 comments

I am unable to replicate with latest version. Try wiping node_modules and pulling in the latests changes on upstream?

Hi @quicksnap, I'm new to npm so I'm not sure what "pulling in the latests changes on upstream" means. Care to enlighten me? I went ahead and deleted node_modules and reran "npm install" and now I don't see the issue anymore. Thanks!

Shit, I was wrong. _npm run dev_ works fine, _npm run start_ keeps giving me the same error.

So after running "npm run dev" you need to run "npm run build" to run "npm start". So this is what I believe is happening (someone should correct me if I'm wrong). When you run npm run dev it creates a new webpack-asset.json which helps point the app to get the resources from webpack dev server. However npm start doesn't use the webpack server (since the webpack server is used for development) the webpack-asset.json is still using the same file as the webpack-asset.json. So what you need to do is run npm run build to re-build the app so that the webpack-asset.json points back to the correct dist folder for production mode.

Hi @k2truong, it's not working regardless. Here are my steps (based on your comment that webpack-assets.json is the problem):

rm webpack-assets.json
npm run build
npm run start

It then shows the regular "running" messages:
...
[1] ==> 🌎 API is running on port 3030
[1] ==> 💻 Send requests to http://localhost:3030
[0] ----
[0] ==> ✅ React Redux Example is running, talking to API server on 3030.
[0] ==> 💻 Open http://localhost:8080 in a browser to view the app.
...
And then the TypeError happens.

+1 same error.
Very strange. It happens sometimes even if you have revert your code to last right commit.
I doubt some bugs with npm packages.

Error Log:

webpack built 1b5f086792d3f966314e in 19949ms
[1] ----
[1] ==> ✅  React Redux Example is running, talking to API server on 3030.
[1] ==> 💻  Open http://localhost:3000 in a browser to view the app.
[1] [piping] can't execute file: /Users/isaac/workspace/srk/uhs/bin/server.js
[1] [piping] error given was: TypeError: res.writeHead is not a function
[1]     at ProxyServer.<anonymous> (server.js:55:9)
[1]     at ProxyServer.emit (/Users/isaac/workspace/srk/uhs/node_modules/eventemitter3/index.js:117:27)
[1]     at ClientRequest.onOutgoingError (/Users/isaac/workspace/srk/uhs/node_modules/http-proxy/lib/http-proxy/passes/ws-incoming.js:153:16)
[1]     at emitOne (events.js:77:13)
[1]     at ClientRequest.emit (events.js:169:7)
[1]     at Socket.socketOnEnd (_http_client.js:294:9)
[1]     at emitNone (events.js:72:20)
[1]     at Socket.emit (events.js:166:7)
[1]     at endReadableNT (_stream_readable.js:905:12)
[1]     at doNTCallback2 (node.js:450:9)
[1] [piping] further repeats of this error will be suppressed...

I know WHY!!!! You must have run some other NodeJS App which use app cache on port 3000, and your browser must be Chrome!
It's ok when use Safari!!!!
@alvaro1728

It relays on webpack-assets.json too.
If I modify some code in src/containers/App/App.js without delete webpack-assets.json, the error occurs;
if I delete webpack-assets.json, then run npm run dev, everything is ok in Safari.

Thanks for confirming there's a problem, @tearsofphoenix. Misery loves company. :-)

For now I'm working around it by setting DISABLE_SSR to true.

I encountered this as well, and put it down to another process running on port 3030.

+1, I got this error too

I was plagued by this error for several days. Removing the following from server.js fixed it for me.

if (!res.headersSent) { res.writeHead(500, {'content-type': 'application/json'}); }

Closing; dupe of #965.

Sorry, I got confused! Wrong issue

@stalkingbutler I also run into this problem whenever I change the server side code. Instead of removing the line all together, I added a check to see if res.writeHead is a function. So far so good.

if (!res.headersSent) {
  if (typeof res.writeHead === 'function') res.writeHead(500, {'content-type': 'application/json'});
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bdsabian picture bdsabian  Â·  6Comments

glennr picture glennr  Â·  6Comments

jorgehmv picture jorgehmv  Â·  3Comments

capir picture capir  Â·  4Comments

sunkant picture sunkant  Â·  4Comments