Script report a referenceError

hi erveryone!

I run my script and report an error!

this is my script

test.js

import grpc from "k6/net/grpc";
import { check } from 'k6';

export let options = {
  scenarios: {
    contacts: {
      executor: 'per-vu-iterations',
      vus: 1,
      iterations: 1,
      maxDuration: '1s',
    },
  },
};

export default () => {  
  console.log(Buffer.from("Hello World").toString('base64'));
}

i run k6 run test.js

and then report
ERRO[0000] ReferenceError: Buffer is not defined

Hi @bobobkb, welcome to the community forum :confetti_ball:

As the message points out Buffer is not defined, as it is a nodejs thing and k6 isn’t nodejs or based on it. I guess what you want is to base64encode stuff, in which case you can use the encoding package.

It does have some problems that you might run into, but they seem to be only with binary data, so hopefully, you won’t be affected :slight_smile:

edit: You can also see how to use ArrayBuffer to do the same here, although that isn’t needed if you have a string as the original form

If you just need base64, then the k6/encoding package is better. But for a generic substitute of the NodeJS-specific Buffer, you probably should use ArrayBuffer: ArrayBuffer - JavaScript | MDN

it works for me,thinks