반응형

서버의 로그을 살펴보던 도중 아래와 같은 에러 문구가 있는것을 발견했다.

 

(node:15479) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security
and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(),
or Buffer.from() methods instead.

해당 에러가 뜨는 이유와 더이상 뜨지 않게 코드를 리팩토링해보자.

 

환경

node.js: 16.x

 

원인

원인은 기존 코드에서 버퍼를 생성할때 아래와 같은 코드로 생성하고 있기 때문이였다.

const testBuffer = new Buffer('test');

 

 

해결

현재 사용하는 node.js 버전에서는 deprecated 되었기 때문에 아래와 같은 방식으로 대체해서 사용해야 한다.

const testBuffer = new Buffer(10) // 기존
const testBuffer2 = Buffer.alloc(10) // 신규

const testBuffer3 = new Buffer('test') // 기존
const testBuffer4 = Buffer.from('test') // 신규

const testBuffer5 = new Buffer('test', 'utf-8') // 기존
const testBuffer6 = Buffer.from('test', 'utf-8') // 신규

위와 같은 코드 형식으로 리팩토링한 후 서버의 에러로그는 사라졌다.

 

 

출처

 

DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server

Getting error when script move to other server. (node:15707) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.

stackoverflow.com

 

반응형
얼은펭귄