Node request causing msecs must be a number Node 4.0

Posted: Oct 07, 2015 by Bryan Tong

If you are seeing this error then it is because of the change made here: https://github.com/nodejs/node-v0.x-archive/commit/f34757398fcc393685b4dfbcbdc692fb38332d6c

Uncaught TypeError: msecs must be a number
      at Object.exports.enroll (timers.js:156:11)
      at Socket.setTimeout (net.js:329:12)
      at ClientRequest.<anonymous> (_http_client.js:558:10)
      at ClientRequest.g (events.js:199:16)
      at ClientRequest.emit (events.js:129:20)
      at tickOnSocket (_http_client.js:486:7)
      at _http_client.js:497:7
      at process._tickCallback (node.js:355:11)

Basically you are no longer allowed to submit a non number value to any timeout values.

In our code we were running the npm request module and by default setting the timeout to null. This will cause this error to be thrown.

I recommend choosing a value that is somewhat sane like 2 minutes or 120000 msecs. You can easily set the default using request like follows.

var request = require('request')
request = request.defaults({timeout: 120000})

If you are setting the value from a config file or environment variable. Make sure and coerce the variable to a number such as.

var config = {timeout: '1000'}
var request = require('request')
request = request.defaults({timeout: +config.timeout})

This should stop the error, give it a try!