question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: The "listener" argument must be of type Function. Received type object

See original GitHub issue

What is the expected behavior? It should work!

What is the actual behavior? It crashes

Possible solution no idea, yet

How to reproduce the issue/test case

const http = require('http');
const https = require('https');

const nock = require('nock');
nock('https://httpbin.org').persist()
  .post('/post').reply(200, (uri, reqBody) => {
    return {
      "hits": {
        "total": 0
      }
    }
  });

const fetch = (url, o) => new Promise((resolve, reject) => {
  let body = o && o.body;
  if (typeof body === 'object') {
    o.headers = { ...o.headers, 'content-type': o.headers && o.headers['content-type'] || 'application/json' };
    body = JSON.stringify(body);
  }
  const req = (/^https:/.test(o && o.protocol || url) ? https : http).request(url, o);
  req.once('error', reject);
  req.once('response', res => {
    const bufs = [];
    res.on('data', buf => bufs.push(buf));
    res.on('end', () => {
      const text = Buffer.concat(bufs);
      if (res.headers.location) return fetch(res.headers.location, o || url).then(resolve)
      resolve(/^application\/json/.test(res.headers['content-type']) ? JSON.parse(text) : text);
    });
    res.on('error', reject);
  });
  if (body && typeof body.pipe === 'function') return body.pipe(req);
  if (body) {
    req.write(body);
  }
  req.end();
});

(async () => {
  await fetch('https://httpbin.org/post', { method: 'POST', body: { wat: 1 } }).then(console.log, console.error);
})()

Versions

Software Version(s)
Nock 10.0.0
Node 10.11.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
JordanShurmercommented, Oct 30, 2018

I’m fairly certain this is due to the change in node’s http api with version 10 (which is LTS as of today, btw).

compare http 8.x with http 10.x

1reaction
gr2mcommented, Oct 9, 2018

I won’t be able to look into it myself, if you could give it a try that’d be great. Or maybe someone else can help out. It’s hacktoberfest, plenty of folks looking to contribute right now 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

The "listener" argument must be of type Function - Stack ...
The reason here is syntax error, you are closing bracket before passing the callback method. Solution: bot.on("message", function(message) { ...
Read more >
"listener argument must be of type function" error with v6.2.2+
I'm receiving an error when upgrading to version 6.2.2 of the launchdarkly-node-server-sdk library. Error Message: The \"listener\" argument ...
Read more >
The "listener" argument must be of type function. Received ...
So im making a discord bot and i am getting this error if running TypeError [ERR_INVALID_ARG_TYPE]: The “listener” argument must be of type ......
Read more >
TypeError: The "listener" argument must be of type Function ...
[image]
Read more >
The “listener” argument must be of type Function. Something ...
I cannot replicate the error. If I take the code from your question, add the two require calls, and run it, I don't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found