sematext / logsene-js

Log shipping library for sending logs to Sematext from JavaScript apps
https://sematext.com/cloud
Apache License 2.0
10 stars 9 forks source link

Module require incompatibility of node-fetch starting in 1.1.76 #54

Open ryprice opened 1 year ago

ryprice commented 1 year ago

During a recently update from 1.1.75 -> 1.1.76, my service started erroring at runtime with this message:

TypeError: fetch is not a function
    at Logsene.send (/path/to/build/service.js:112653:3)
    at Timeout._onTimeout (/path/to/build/service.js:112351:12)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

This build uses typescript and webpack. I poked around the transpiled output, console logged fetch, and found that the imported value of fetch from node-fetch was actually a module, not a fetch function. It looked something like: {FetchErrors, Headers, Request, Response, default}. So it seems somewhere there's a mismatch about the style of module (commonjs, esm, etc).

If it's of interest the transpiled line looked like this:

const fetch = __webpack_require__(/*! node-fetch */ "./node_modules/node-fetch/lib/index.mjs")

I'm not the most well versed in the different types of module bundling so I'm not sure what the fix here is, or if it's an issue in my build or in logsene-js configs. I should also note I have another library gaxios that also depends on node-fetch and it does not have any issues. The difference seems to be that gaxios has compiled lib files bundled with it.

Thanks for your help.

otisg commented 1 year ago

@ryprice Thanks for the report, we'll have a look. Are you shipping your logs to Sematext or somewhere else?

ryprice commented 1 year ago

I'm shipping to sematext. Just a small sideproject I'm working on.

regiluze commented 1 year ago

hi @ryprice we've just released a new version of the library --> https://github.com/sematext/logsene-js/releases/tag/1.1.77 Please, try it out and tell us if the issue has been removed. Thanks :)

ryprice commented 1 year ago

Hey, thanks for the quick turnaround, but unfortunately this didn't fix the problem. Now it just says nodeFetch is not a function. If I have time I'll try to figure out what needs to be done or at least make a distilled repro for you. The issue in my case isn't a conflict with window (In fact my env is node so window.fetch shouldn't be defined. logsene-js works fine in my frontend apps.)