Fix 'util.pump is not a function' in Node.js
By Flavio Copes
Learn how to replace the removed util.pump function with the current Node.js stream pipeline API, using promises or a callback that handles errors.
The util.pump is not a function error means the code uses an old Node.js API.
Old programs used this syntax to send a readable stream into a writable stream:
util.pump(readableStream, writableStream)
util.pump() was deprecated and then removed. Use pipeline() from the node:stream module instead.
Use the promise API
In an ES module, import pipeline from node:stream/promises:
import { pipeline } from 'node:stream/promises'
try {
await pipeline(readableStream, writableStream)
} catch (error) {
console.error('The pipeline failed', error)
}
pipeline() connects the streams, forwards errors, and cleans them up when the operation fails.
Use the callback API
CommonJS programs can use the callback version:
const { pipeline } = require('node:stream')
pipeline(readableStream, writableStream, error => {
if (error) {
console.error('The pipeline failed', error)
return
}
console.log('The pipeline completed')
})
Do not leave the callback empty. An empty callback hides the error that pipeline() reports.
See the current pipeline() documentation for more stream types and options.
Related posts about node: