Using k6 in Typescript

I try to install k6 by ''npm install --save @types/k6"
But I met an error when running ‘tsc’
node_modules/@types/k6/global.d.ts:69:9 - error TS2451: Cannot redeclare block-scoped variable ‘console’.
How I can fix this issue?

@CongNguyen I haven’t been able to reproduce this error. See below the installation output.

$ npm install --save @types/k6

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN exp@1.0.0 No description
npm WARN exp@1.0.0 No repository field.
@types/k6@0.28.3
added 1 package from 5 contributors and audited 1 package in 5.182s
found 0 vulnerabilities

In any case, if you want to use TypeScript in your k6 scripts, I recommend using the TypeScript project template.

1 Like

I have another question if I just want to use JS, after enter the cmd below

npm install --save @types/k6

why I can’t use npx to run k6?

npx k6 run ./xxx.js

Do i need to do the webpack? or any example i can follow?

Hey,

k6 isn’t really nodejs, and as such, npx won’t help you in this situation. What you’re installing using npm install --save-dev @types/k6 is only the typescript type information.

If you only want to use js, and don’t want to use any external modules that require transpilation or polyfilling, skipping webpack is totally fine.

Best,
Simme

2 Likes

oh… that’s the reason why NPX is not working. thanks @simme

your answer is very useful.