• Home
  • Argument passed in must be a single string of 12 bytes or a string of 24 hex characters – Node JS

Argument passed in must be a single string of 12 bytes or a string of 24 hex characters – Node JS

This error usually occurs when you are trying to use the Buffer class in Node.js to create a buffer from a string or an array of bytes, and the string or array of bytes is not in the correct format.

To fix this error, you can try the following:

  1. Make sure that you are passing in a string of 12 bytes or a string of 24 hex characters to the Buffer constructor. You can use the Buffer.byteLength method to check the length of the string.
  2. If you are passing in an array of bytes, make sure that it contains only valid byte values (i.e., integers between 0 and 255).
  3. If you are passing in a string of hex characters, make sure that it contains only valid hex characters (i.e., the characters ‘0’ through ‘9’ and ‘a’ through ‘f’).
  4. If you are trying to create a buffer from a string that is not in the correct format, you can try using the Buffer.from method instead of the Buffer constructor. The Buffer.from method can take a string and an encoding parameter, and it will create a buffer from the encoded version of the string.

For example:

const buffer = Buffer.from('hello world', 'utf8');

This will create a buffer containing the UTF-8 encoded version of the string ‘hello world’.