Skip to content

bufferAllocators

Prefer modern Buffer allocation methods over the deprecated Buffer constructor.

✅ This rule is included in the node logical presets.

The new Buffer() constructor has been deprecated since Node.js 4 due to security and usability concerns. Modern alternatives provide safer and more explicit ways to create buffers.

Use Buffer.from() for creating a Buffer from existing data (strings, arrays, or ArrayBuffers). Use Buffer.alloc() for creating a Buffer of a specific size with zero-filled memory.

const buffer = new
var Buffer: BufferConstructor
new (str: string, encoding?: BufferEncoding) => Buffer<ArrayBuffer> (+3 overloads)

Allocates a new buffer containing the given {str}.

@paramstr String to store in buffer.

@paramencoding encoding to use, optional. Default is 'utf8'

@deprecatedsince v10.0.0 - Use Buffer.from(string[, encoding]) instead.

Buffer
("7468697320697320612074c3a97374", "hex");
Error ts(2451) ― Cannot redeclare block-scoped variable 'buffer'.
const buffer = new
var Buffer: BufferConstructor
new (array: ArrayLike<number>) => Buffer<ArrayBuffer> (+3 overloads)

Allocates a new buffer containing the given {array} of octets.

@paramarray The octets to store.

@deprecatedsince v10.0.0 - Use Buffer.from(array) instead.

Buffer
([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
Error ts(2451) ― Cannot redeclare block-scoped variable 'buffer'.
const buffer = new
var Buffer: BufferConstructor
new (size: number) => Buffer<ArrayBuffer> (+3 overloads)

Allocates a new buffer of {size} octets.

@paramsize count of octets to allocate.

@deprecatedsince v10.0.0 - Use Buffer.alloc() instead (also see Buffer.allocUnsafe()).

Buffer
(10);
Error ts(2451) ― Cannot redeclare block-scoped variable 'buffer'.

This rule is not configurable.

If you are working with legacy code that specifically requires the old Buffer constructor (which is highly unlikely in modern codebases), you might choose not to enable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.