In my project, I have a large mess of related code that I don’t want in my main file. Is there a way to put it in another file and import+run it in the middle of my main file? The code needs to be able to access previously defined stuff, and define stuff accesseble outside the new file.
This is sort of what I mean:
// main.js
var food = 'pickles';
console.log('a');
magicFileImport('new.js');
console.log(text);
// new.js
console.log('b');
var text = `I like ${food}`;
If this is in browser js and not node, you can link both js files in your html, and every js file should be able to access variables and functions from all files linked before them, so you’d link the “new.js” before the “main.js”.
I’ve edited the example to show you how the ordering works; I want new.js to run in the middle of main.js, and then continue running main.js when it finishes.