This error occurs when you pass an invalid entry type to PerformanceObserver.observe() in Node.js's perf_hooks module. The observer only accepts specific, predefined entry types.
This TypeError is thrown by Node.js's `perf_hooks` module when you attempt to observe a performance entry type that doesn't exist or isn't supported. The PerformanceObserver API is designed to watch specific performance metrics like marks, measures, and garbage collection events, but it only accepts a predefined set of entry type strings. When you call `observe()` with an invalid entry type in the `type` or `entryTypes` option, Node.js validates the provided string against its internal list of supported types. If the type doesn't match any known entry type, it throws this TypeError immediately. This is a safeguard to catch typos and configuration errors early, preventing you from setting up observers that would never receive any performance data.
Check the [Node.js perf_hooks documentation](https://nodejs.org/api/perf_hooks.html) for your specific Node.js version to see all supported entry types.
Common valid entry types in Node.js include:
- 'mark' - Performance marks created with performance.mark()
- 'measure' - Performance measures created with performance.measure()
- 'function' - Function execution timing
- 'gc' - Garbage collection events
- 'http' - HTTP request/response timing
- 'http2' - HTTP/2 request/response timing
- 'dns' - DNS lookup timing
If you're using a type not in this list, verify it exists in your Node.js version.
Review your observe() call for spelling errors:
// ❌ WRONG - typo in entry type
const obs = new PerformanceObserver((list) => {
console.log(list.getEntries());
});
obs.observe({ entryTypes: ['measur'] }); // TypeError!
// ✅ CORRECT - proper spelling
obs.observe({ entryTypes: ['measure'] });Entry types are case-sensitive, so 'Measure' or 'MEASURE' won't work.
The observe() method accepts either type (string) or entryTypes (array), but not both:
// ✅ CORRECT - using type (single entry)
obs.observe({ type: 'measure' });
// ✅ CORRECT - using entryTypes (multiple entries)
obs.observe({ entryTypes: ['mark', 'measure'] });
// ❌ WRONG - mixing both options
obs.observe({
type: 'measure',
entryTypes: ['mark', 'measure']
});If you use type, provide a single string. If you use entryTypes, provide an array of strings.
Some entry types were added in specific Node.js versions. Check your Node.js version:
node --versionThen verify the entry type is supported in that version. For example:
- 'http' and 'http2' were added in later versions
- Some experimental entry types may require specific flags
If you need a newer entry type, consider:
- Upgrading Node.js to a version that supports it
- Using a fallback or alternative monitoring approach
- Wrapping the observer setup in a try-catch with version detection
Create a simple test to isolate the issue:
import { PerformanceObserver } from 'perf_hooks';
// Test each entry type individually
const validTypes = ['mark', 'measure', 'function', 'gc'];
validTypes.forEach((type) => {
try {
const obs = new PerformanceObserver((list) => {
console.log(`${type} entries:`, list.getEntries());
});
obs.observe({ type });
console.log(`✓ ${type} is supported`);
} catch (err) {
console.error(`✗ ${type} failed:`, err.message);
}
});This will show you exactly which entry types work in your environment.
Entry Type Availability by Context
Not all entry types are available in every Node.js context:
- 'gc' entries require the --expose-gc flag or calling performance.gc() with appropriate permissions
- 'dns' entries are only emitted for DNS operations performed through Node.js's dns module
- 'http' and 'http2' entries require using Node.js's built-in HTTP modules
Performance Impact Considerations
When setting up performance observers:
- Observing 'gc' can have overhead as it tracks every garbage collection cycle
- Using buffered: true retrieves past entries, which may include many items
- Multiple observers watching the same entry types each create separate callback invocations
Dynamic Entry Type Validation
For production code that needs to work across Node.js versions, implement dynamic validation:
import { PerformanceObserver, constants } from 'perf_hooks';
function supportsEntryType(type: string): boolean {
try {
const obs = new PerformanceObserver(() => {});
obs.observe({ type });
obs.disconnect();
return true;
} catch {
return false;
}
}
// Use this to conditionally set up observers
const entryTypes = ['mark', 'measure', 'gc'].filter(supportsEntryType);
if (entryTypes.length > 0) {
const obs = new PerformanceObserver((list) => {
// Handle entries
});
obs.observe({ entryTypes });
}This approach ensures your monitoring code degrades gracefully rather than crashing on unsupported entry types.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams