I ran into this issue when trying to import the latest minified release of faker.js.
Example:
// test.js
import faker from "https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js";
export default function() {
console.log(faker.name.findName())
}
Running k6 run -v test.js outputs:
DEBU[0000] Loading... moduleSpecifier="file:///home/ivan/.local/go/src/github.com/loadimpact/k6/test.js" original moduleSpecifier=test.js
DEBU[0000] Babel: Transformed t=37.284193ms
DEBU[0000] Loading... moduleSpecifier="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js" original moduleSpecifier="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js"
DEBU[0000] Fetching source... url="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js?_k6=1"
DEBU[0000] Fetched! len=1015294 t=214.483039ms url="https://raw.githubusercontent.com/Marak/faker.js/master/build/build/faker.min.js?_k6=1"
DEBU[0001] Loading... moduleSpecifier=//1000 original moduleSpecifier=1000
WARN[0001] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme url=//1000
DEBU[0001] Fetching source... url="https://1000?_k6=1"
DEBU[0001] Fetching source... url="https://1000"
ERRO[0001] GoError: The moduleSpecifier "1000" couldn't be retrieved from the resolved url "https://1000". Error : "Get https://1000: dial tcp: lookup 1000: no such host"
Note that this doesn't happen with non-minified releases, or the older minified version from CDNJS (https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js), which work fine.
It seems that the 1e3 (scientific notation for 1000) dependency is incorrectly interpreted as a URL at some point, which k6 attempts to load and fails.
Strangely enough, manually changing the 1e3 reference in the minified JS to 1000 fixes the issue, so this seems like either Goja or k6 misinterpreting scientific notation, for some reason. A quick test of vm.RunString("1 + 1e3") correctly returns 1001, however, so this must be something deeper with how it resolves imports.
I'm using the k6 docker image with Typescript-compiled scriptsw and I'm seeing a similar issue trying to import local packages such as chai.
import chai from 'chai';
...
chai.assert(2 > 1);
Produces this at runtime:
WARN[0000] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme url=//chai
ERRO[0000] GoError: The moduleSpecifier "chai" couldn't be retrieved from the resolved url "https://chai". Error : "Get https://chai: dial tcp: lookup chai on 127.0.0.11:53: no such host"
Similarly, if I enable Typescript's "importHelpers": true flag in my tsconfig.json, I get a very simillar warning:
WARN[0000] A url was resolved but it didn't have scheme. This will be deprecated in the future and all remote modules will need to explicitly use `https` as scheme url=//tslib
ERRO[0000] GoError: The moduleSpecifier "tslib" couldn't be retrieved from the resolved url "https://tslib". Error : "Get https://tslib: dial tcp: lookup tslib on 127.0.0.11:53: no such host"
@jedkcanderson, I'm not very familiar with TypeScript, but it's normal for import chai from 'chai'; to not work in k6. k6 is not Node.js, nor is it based on Node.js, so it doesn't support the Node.js module resolution algorithm or anything like it.
You have to use something like import chai from './chai.js';.
Also, k6 does not support TypeScript (#422), but you can somewhat mitigate both problems by using my k6-es6 project which uses nodejs, webpack and babel to combine your script in one single file and do the nodejs module resolution, as well as polyfill what is needed for anything that comes from npm for you ... which hopefully will make it work.
You can even (if using the current master) use --compatibility-mode=base with the final script for some perfomance gains ;)
You will need to add typescript support and your dependency in package.json as usual.
This was fixed by https://github.com/dop251/goja/issues/221, now we should only need to update goja (and test to double-check) to close this...
I tested with
// test.js
import faker from "https://raw.githubusercontent.com/Marak/faker.js/9c65e5dd4902dbdf12088c36c098a9d7958afe09/dist/faker.min.js";
export default function() {
console.log(faker.name.findName())
}
as the path has changed.. I decided to pin the current latest version with its commit. This still breaks with the same error on 0.28.0 but works with the changes from #1707
Most helpful comment
I tested with
as the path has changed.. I decided to pin the current latest version with its commit. This still breaks with the same error on 0.28.0 but works with the changes from #1707