'how to get path for import of javascript from file

I have a file where i defined the absolute path of directory.

Ex : script=/absolutepath/scripts
utility=/absolutepath/utility

I want to use "script"/"utility" instead of absolute path in other javascript files.How i can do this.

What i want :

import random from "script/random.js"

instead of

import random from "/absolutepath/scripts/random.js"

PS :I am using k6 load generating framework which doesn't support node modules.



Solution 1:[1]

You currently can't do that in k6 v0.26.0.

Import paths like that are reserved for internal k6 modules (e.g. k6/http) and "magic" remote import URLs (e.g. import from github.com/loadimpact/k6/samples/thresholds_readme_example.js instead of https://raw.githubusercontent.com/loadimpact/k6/master/samples/thresholds_readme_example.js, and we're trying to softly discourage this). You can't define your own, you either have to use relative paths or absolute paths when importing your own JS files.

Solution 2:[2]

You could try using importmaps.

Inside your index.html:

<html lang="en">

<head>
    <script type="importmaps">
        {
            "imports": {
                "random": "/absolutePath/scripts/random.js"
        }
    }
    </script>
    <script type="module" src="app.js"></script>
</head>

</html>

You can now import your module from ANYWHERE

import random from 'random'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 na--
Solution 2