How can I import/require a uploaded JS File into my javascript

Viewed 38

I want to be able to upload some JS Files (Self-Programmed Bots for a game) and then be able to execute the code inside them. Currently the User is required to have the Programm in a certain directory and I take the file from there like this:

this.bot = require('../bots/' + file.name); 

Is there a better way to do this that, if possible, also works when the Website is running on a Server (I'm not sure if my current solution would)?

1 Answers

try like this

index.html

<!DOCTYPE html>
    <html>
        <head>
    
        </head>
        <body>
            <script type="module" src="script2.js"></script>
        </body>
    </html>

script1.js

export default function Hello() {
    return "Hello buddy!";
}

script2.js

import { Hello } from './script1.js';
let val = Hello;
console.log(val);
Related