-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem in importing chain of modules #778
Comments
You don't need to append I think the issue here may be because k6 doesn't read the Seems like it should be easy enough to implement if we want to support the "mainstream" behavior of reading the @robingustafsson, thoughts? @kkeranen, since I'm not terribly familiar with the whole huge JS ecosystem, any corrections, examples or documentation links related to this would be much appreciated! |
Related/duplicate issue: #475 |
Thank you @na for reply. However, I don't see that this is related to index.js. Let me explain what I'm trying to do. We are using libraries originally written in TypeScript and then compiled to JavaScript. So, one of the simple cases was using guid-typescript npm package and importing it to our main script.js by This didn't work. Got runtime error:
I got this working by changing that line to: Note that if I remove ".js" from that line, it doesn't work anymore, so this is not working: I can't apply that kind of fix to all other cases that are more complex, containing imports in chains. |
Hmm that should still be mostly doable. When I look at node's documentation on module loading, if
Basically the big challenge here is to support a lot of the npm dependency management and resolution functionality in combination with k6-specific features like importing scripts from URL, and without actually confusing users or fully supporting node.js libraries... |
@kkeranen, as a workaround until we improve the k6 module loader to support some of the module resolution used by node.js, can you try to use some project that bundles script dependencies in a single file and use that? As I said, I'm not very familiar with the whole JS ecosystem, but IIRC browserify does that and there seem to be other projects that just merge modules like this and this. |
The merge module tools you linked seem to combine nested package.json files into one merged package.json, so I don't see how they could help. Then the k6 requirement to use |
I've tried to strip down all dependencies to progress in my work, but there are still some basic things like creating GUID that I can't use in easy way because import chain doesn't work. |
You need a GUID or an UUID4? If you need a UUID4 here you go: /**
* Fast UUID generator, RFC4122 version 4 compliant.
* @author Jeff Ward (jcward.com).
* @license MIT license
* @link http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
**/
var UUID = (function() {
var self = {};
var lut = []; for (var i=0; i<256; i++) { lut[i] = (i<16?'0':'')+(i).toString(16); }
self.generate = function() {
var d0 = Math.random()*0xffffffff|0;
var d1 = Math.random()*0xffffffff|0;
var d2 = Math.random()*0xffffffff|0;
var d3 = Math.random()*0xffffffff|0;
return lut[d0&0xff]+lut[d0>>8&0xff]+lut[d0>>16&0xff]+lut[d0>>24&0xff]+'-'+
lut[d1&0xff]+lut[d1>>8&0xff]+'-'+lut[d1>>16&0x0f|0x40]+lut[d1>>24&0xff]+'-'+
lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+lut[d2>>16&0xff]+lut[d2>>24&0xff]+
lut[d3&0xff]+lut[d3>>8&0xff]+lut[d3>>16&0xff]+lut[d3>>24&0xff];
}
return self;
})(); |
This is a nice feature but I think JS developers are not expecting this API. The dynamic To me, I would be totally fine with dropping the support of this feature if it provides a benefit to the future of the k6 module resolution system. |
As I mentioned in #475 (comment), we're very unlikely to adopt the node module resolution algorithm in k6. If someone needs nested NodeJS modules with k6, using webpack (or any of the other bundlers) to resolve and bundle them should be very easy: https://github.com/k6io/template-es6 Dynamic |
As per your documentation one needs to add ".js" to paths when importing external modules.
I'm trying to import modules that try to import other modules (I explained this also in comment of another issue), and only way I can get this to work is to add ".js" into import statements in the whole import chain. However, this is very hard for two reasons: 1) some modules are 3rd party modules and 2) our modules are compiled to JavaScript from TypeScript.
Am I doing something wrong, or is using of chained modules (test script importing module1, then module1 importing module 2 and so on) really so hard? It's so hard that I have not been able to complete the work yet and I've started to consider alternatives to k6.
The text was updated successfully, but these errors were encountered: