For my k6.io tests, I am trying to import TextDecoder from the util package.
Within my script, I aim to read a binary file:
import { sleep, check } from 'k6';
import { Options } from 'k6/options';
import http from 'k6/http';
/* @ts-ignore */
import { TextDecoder } from 'util';
export const options:Options = {
vus: 1,
iterations: 1,
};
const file = open('uuids.txt', 'b');
export function setup() {
console.log("File is " + file);
let enc = new TextDecoder("utf-8")
console.log("Decode: " + enc.decode(file));
}
export default () => {
.....post HTTP Request
}
In my package.json, I have included 'util' (npm install util --save-dev):
{
"name": "typescript",
"version": "1.0.0",
"main": "index.js",
"repository": "ssh://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60070914200709140815024e030f0d">[email protected]</a>/k6io/example-typescript.git",
"author": "Simon Aronsson <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13607a7e7e765378253d7a7c">[email protected]</a>>",
"license": "MIT",
"devDependencies": {
"@babel/core": "7.13.16",
"@babel/plugin-proposal-class-properties": "7.13.0",
"@babel/plugin-proposal-object-rest-spread": "7.13.8",
"@babel/preset-env": "7.13.15",
"@babel/preset-typescript": "7.13.0",
"@types/k6": "~0.41.0",
"@types/node": "^20.4.5",
"@types/webpack": "5.28.0",
"babel-loader": "8.2.2",
"clean-webpack-plugin": "4.0.0-alpha.0",
"copy-webpack-plugin": "^9.0.1",
"typescript": "4.2.4",
"util": "^0.12.5",
"webpack": "5.61.0",
"webpack-cli": "4.6.0",
"webpack-glob-entries": "^1.0.1"
},
"scripts": {
"start": "webpack"
},
"browser": {
"fs": false,
"path": false,
"os": false
}
}
However, when attempting to run my script with 'k6 run', it appears that the util package was not resolved. Can you provide insight into why this may be happening?
k6 run dist\post-201-create-node-test.js
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
ERRO[0000] ReferenceError: process is not defined
at 539 (webpack://typescript/./node_modules/util/util.js:109:0(78))
at __webpack_require__ (webpack://typescript/webpack/bootstrap:19:0(26))
at webpack://typescript/external%20commonjs%20%22https://jslib.k6.io/k6-utils/1.4.0/index.js%22:1:34(47)
at file:///C:/Dev//dist/post-201-create-node-test.js:2323:3(71)
at file:///C:/Dev/dist/post-201-create-node-test.js:2328:12(3) hint="script exception"
----------------- RESOLVED ----------------
To resolve this issue, I utilized my test data using SharedArray:
const data = new SharedArray('some data name', function () {
return JSON.parse(open('./data.json')).uuids;
});