I'll help you simulate a Node.js stream error that might occur with Node 14.16. Here's an example that demonstrates a common stream error scenario:
// stream-error-example.js
const { Transform } = require('stream');
const fs = require('fs');
// Create a problematic transform stream
class ProblematicTransform extends Transform {
_transform(chunk, encoding, callback) {
// Simulate an error condition
if (chunk.toString().includes('ERROR')) {
// This will cause an unhandled error in the stream
this.emit('error', new Error('Stream processing failed'));
return;
}
// Normal processing
this.push(chunk.toString().toUpperCase());
callback();
}
}
// Example 1: Unhandled stream error
function causeStreamError() {
const readStream = fs.createReadStream('input.txt');
const transform = new ProblematicTransform();
const writeStream = fs.createWriteStream('output.txt');
readStream
.pipe(transform)
.pipe(writeStream);
// This will crash if input.txt contains "ERROR"
}
// Example 2: Common package-related stream error
const zlib = require('zlib');
function causeZlibStreamError() {
const gzip = zlib.createGzip();
// Writing invalid data after stream is closed
gzip.end();
setTimeout(() => {
try {
gzip.write('This will cause an error');
} catch (err) {
console.error('Caught error:', err.message);
}
}, 100);
}
// Example 3: HTTP stream error with axios or node-fetch
const http = require('http');
function causeHttpStreamError() {
const server = http.createServer((req, res) => {
// Abruptly destroy the response stream
setTimeout(() => {
res.destroy();
}, 50);
});
server.listen(3000, () => {
http.get('http://localhost:3000', (res) => {
res.on('data', (chunk) => {
console.log('Received:', chunk);
});
res.on('error', (err) => {
console.error('Stream error:', err.message);
server.close();
});
});
});
}
// Run examples
console.log('Creating test file...');
fs.writeFileSync('input.txt', 'This is normal text\nERROR: This will cause problems\nMore text');
console.log('\nExample 1: Transform stream error');
try {
causeStreamError();
} catch (err) {
console.error('Stream error:', err);
}
console.log('\nExample 2: Zlib stream error');
causeZlibStreamError();
console.log('\nExample 3: HTTP stream error');
causeHttpStreamError();
Common packages that often cause stream errors in Node 14.16:
- zlib - Compression streams can fail with invalid data
- fs - File streams can fail with permission or disk space issues
- http/https - Network streams can fail with connection issues
- crypto - Cipher streams can fail with invalid keys/data
- Third-party packages:
- multer - File upload streams
- sharp - Image processing streams
- csv-parser - CSV parsing streams
- archiver - ZIP file creation streams
To properly handle stream errors, always add error handlers:
const stream = require('stream');
const { pipeline } = require('stream');
// Better error handling approach
function safeStreamProcessing() {
const readStream = fs.createReadStream('input.txt');
const transform = new ProblematicTransform();
const writeStream = fs.createWriteStream('output.txt');
// Using pipeline for automatic error handling
pipeline(
readStream,
transform,
writeStream,
(err) => {
if (err) {
console.error('Pipeline failed:', err);
} else {
console.log('Pipeline succeeded');
}
}
);
// Or manually handle errors on each stream
readStream.on('error', (err) => console.error('Read error:', err));
transform.on('error', (err) => console.error('Transform error:', err));
writeStream.on('error', (err) => console.error('Write error:', err));
}
The most common stream error messages you might see:
Error: write after end - Writing to a closed stream
Error: stream.push() after EOF - Pushing data after stream ended
Error: Cannot pipe, not readable - Trying to pipe from non-readable stream
Error: ENOENT: no such file or directory - File stream can't find file
Error: EPIPE: broken pipe - Connection closed while writing
To debug which package is causing the error, check the stack trace for the module name, or use NODE_DEBUG=stream environment variable for detailed stream debugging.