AWS Sig v4 - SyntaxError: Unexpected character


I’m trying to use AWS Signature v4 to authenticate my api calls in k6. I have followed the guide, but when i try to run the test locally i get an error pointing to the automatically generated (and enormous) file aws4.js:

ERRO[0000] SyntaxError: file:///C:/Users/tjackenpacken/source/repos/TestApi/tests/aws4.js: Unexpected character '�' (1:0)
> 1 | ��(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.aws4 = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
    | ^
  2 |
  3 | 'use strict';
  4 |
        at <internal/k6/compiler/lib/babel.min.js>:2:28542(109)
        at <internal/k6/compiler/lib/babel.min.js>:14:24413(13)
        at s (<internal/k6/compiler/lib/babel.min.js>:1:1331(8))
        at reflect.methodValueCall (native)
        at file:///C:/Users/tjackenpacken/source/repos/TestApi/tests/the-real-test-copy.js:14:177(41)
        at reflect.methodValueCall (native)
        at file:///C:/Users/tjackenpacken/source/repos/TestApi/tests/hello-world.js:1:107(13)  hint="script exception"

I think it looks like some sort of decoding issue, but i can’t figure out how to solve it. Anyone has any ideas?

How did you download the aws4.js file to your system?

It could be that the encoding is in the first two bytes of the file, and whatever you used to download it did not correctly encode the file as, most likely, UTF-8.

Are you able to download the file using a different technique?

I used npm inside VS code, are you aware of any other technique to download the file?


Ok, so i realised that the install aws4 command generated two different aws4.js files in different locations. But when i’m trying the other one (located in the node_modules-folder) i get following errors:

WARN[0000] The moduleSpecifier "aws4" has no scheme but we will try to resolve it as remote module. This will be deprecated in the future and all remote modules will need to explicitly use "https" as scheme.
ERRO[0000] Module specifier "aws4" was tried to be loaded as remote module by prepending "https://" to it, which didn't work. If you are trying to import a nodejs module, this is not supported as k6 is _not_ nodejs based. Please read for more information. Remote resolution error: "Get "https://aws4": Service Unavailable"

I did ran the browserify node_modules/aws4/aws4.js -s aws4 > aws4.js command without any signs of errors.

Do you mean you ran the following command in the Terminal panel within VS Code?

npm install aws4

Yes, exactly

npm install aws4
1 Like