Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
FetchEventSource
✓ import FetchEventSource from 'fetch-event-source'
✗ const FetchEventSource = require('fetch-event-source')
Default export; CJS require works if using Node.js with the described server path.
FetchEventSource (server)
✓ import FetchEventSource from 'fetch-event-source/server'
✗ import { FetchEventSource } from 'fetch-event-source/server'
For Node.js environments, import from the server subpath.
FetchEventSourceConfiguration
✓ import type { FetchEventSourceConfiguration } from 'fetch-event-source'
✗ import { FetchEventSourceConfiguration } from 'fetch-event-source'
The interface is exported as a type; import it with 'type' modifier to avoid runtime inclusion.
Creates a FetchEventSource with custom headers, auto-start enabled, and event listeners for open, message, and error events.
import FetchEventSource from 'fetch-event-source';
const config = {
url: 'https://example.com/sse',
headers: { Authorization: 'Bearer ' + (process.env.TOKEN ?? '') },
withCredentials: false,
autoStart: true,
};
const eventSource = new FetchEventSource(config);
eventSource.addListener('open', () => console.log('Connected'));
eventSource.addListener('message', (e) => console.log('Data:', e.data));
eventSource.addListener('error', (err) => console.error('Error:', err));
eventSource.start();
Debug
Known issues
gotchaThe library is in early alpha; API may break without major version bump.fixPin to exact version and test upgrades carefully.
affects: >=1.0.0-alpha.1 <1.0.0
gotchaThe constructor expects a configuration object, not a string URL like native EventSource.fixPass a FetchEventSourceConfiguration object with url property instead of a string.
affects: >=0.0.1
gotchaThe 'message' listener receives a MessageEvent-like object, but 'data' is a string (not parsed JSON).fixParse manually: JSON.parse(e.data);
affects: >=0.0.1
gotchaIn Node.js, you must import from 'fetch-event-source/server' to avoid browser-specific APIs.fixUse 'import FetchEventSource from 'fetch-event-source/server';' in Node.js.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: FetchEventSource is not a constructor
Importing the default export incorrectly using named import syntax.
fixUse default import: import FetchEventSource from 'fetch-event-source';
ReferenceError: fetch is not defined
Running in Node.js without a global fetch polyfill (e.g., node-fetch).
fixInstall node-fetch and import it globally, or use the custom fetch in config.
Error: EventSource is not a valid constructor
Passing a string URL instead of a configuration object to the constructor.
fixPass { url: '...', ... } object to new FetchEventSource(config). Upgrade
Version history
1.0.0-alpha.2latest on npm
Audit
Dependencies
No dependency data recorded yet.