Maybe I becoming blind, but I don't see where I am wrong.
I would use commonjs interface of fetch-cookie.
According to the documentation, this is possible, indeed.
https://github.com/valeriangalliat/fetch-cookie?tab=readme-ov-file#development
So, I tried:
const fetchCookie = require('fetch-cookie')
const tough = require('tough-cookie');
const username = 'x';
const password = 'x';
// Create a cookie jar
const cookieJar = new tough.CookieJar();
// Wrap fetch with fetch-cookie and the created cookie jar
const fetchWithCookies = fetchCookie(fetch, cookieJar);
but:
❯ node testfetch.js
const fetchWithCookies = fetchCookie(fetch, cookieJar);
^
TypeError: fetchCookie is not a function
at Object.<anonymous> (/home/user/Documents/testfetch.js:15:26)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
The function you're looking for is exported as default. It's a bit awkward in CommonJS, but you can reference it explicitly:
const fetchCookie = require('fetch-cookie').default;
// Here -----------------------------------^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With