Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch ^4.0.0-beta.1 generating error: TypeError [ERR_INVALID_ARG_TYPE]: The listener argument must be of type function. Received an instance of Object #1620

Open
fluxquantum opened this issue Aug 3, 2022 · 7 comments · May be fixed by #1693
Labels

Comments

@fluxquantum
Copy link

After upgrading from node-fetch 2.6.7 to "node-fetch": "^4.0.0-beta.1." I am seeing errors with the execution of fetch.
After running node bin/www, I am able to spin up my node backend server but then when making a request I see the following error now:

// Full paths ellided
{"message":"Error fetching tokens: TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object\n at checkListener (node:events:259:3)\n at ClientRequest.once (node:events:661:3)\n at new ClientRequest (node:_http_client:215:10)\n at Object.request (node:https:353:10)\n at /node_modules/agent-base/patch-core.js:25:22\n at file:///node_modules/node-fetch/src/index.js:94:20\n at new Promise ()\n at fetch (file://node_modules/node-fetch/src/index.js:49:9)

Reproduction
const fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));

// Variables values ellided
const getTokens = async (code, redirectUrl) => {
const authString = Basic ${Buffer.from(${config.Id}:${config.secret}).toString('base64')};
const fetchTokenURL = tokenUrl + ?code=${code}&grant_type=auth_code&redirect=${redirect};

try {
let response = await fetch(fetchTokenURL, {
agent: new HttpsProxyAgent(httpProxy),
method: 'POST',
headers: {
Authorization: authString,
Accept: 'application/json',
}
})
let data = await response.json();
return data;
} catch (error) {
logger.info("Error fetching tokens: " + error.stack);
}
}

Steps to reproduce the behavior:

  1. Change from const fetch = require("node-fetch"); to const fetch = (...args) =>
    import('node-fetch').then(({ default: fetch }) => fetch(...args));
  2. Run node bin/www

Expected behavior

I should be able to hit my target endpoint without issues after import problem was resolved.

Screenshots

Your Environment
alpine linux

software version
node-fetch ^4.0.0-beta.1
node v16.16.0
npm 8.11.0
Operating System macos

Additional context

@fluxquantum fluxquantum added the bug label Aug 3, 2022
@asbjornenge
Copy link

asbjornenge commented Aug 5, 2022

I just had this issue with node-fetch@3.2.10 when trying to use a https.agent 🤔

Downgraded to 2.6.7 and now it works 🎉

@fluxquantum
Copy link
Author

@asbjornenge appreciate you responding to my post. I actually was using version 2.6.7 as well, but I have to eventually upgrade to 4.x or at least 3.x due to a vulnerability that was reported recently. Unless they are able to release a 2.x patch.

@vyatsun
Copy link

vyatsun commented Oct 1, 2022

I'm also having this issue with version 3.2.9

@guillaume-fr
Copy link

Seems to work when only one parameter is provided to https.request (changing node-fetch code)

		// Send request
		options.url = parsedURL.toString();
		options.host = parsedURL.host;
		const request_ = send(options);

@fluxquantum94
Copy link

Seems to work when only one parameter is provided to https.request (changing node-fetch code)

		// Send request
		options.url = parsedURL.toString();
		options.host = parsedURL.host;
		const request_ = send(options);

Hi @guillaume-fr , thanks for responding. So what do your imports look like now? Are you saying you had to essentially side step using the feature of node-fetch?

@guillaume-fr
Copy link

I just went in node_modules folder and fixed this inside node-fetch/src/index.js. You may search for // Send request comment to find where this is located.

Had to do this in place as node-fetch is included as a dependency of another package I'm importing and I just wanted to see if this is easily fixable. Obviously this is not applicable as-is for a production scenario. You would have to find a better way to inject this workaround or wait for an actual fix in this repo.

It would be interesting to understand why it fails. It looks like calling with url and options as separated arguments should be allowed. We can avoid the error by providing only one argument but I don't know what's the root cause.

guillaume-fr added a commit to guillaume-fr/node-fetch that referenced this issue Dec 22, 2022
@guillaume-fr guillaume-fr linked a pull request Dec 23, 2022 that will close this issue
@guillaume-fr
Copy link

guillaume-fr commented Dec 23, 2022

@fluxquantum94 you can try my PR changing the version in your package.json:
"node-fetch": "guillaume-fr/node-fetch",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants