0

let's say I have some Javascript code living on a webserver that has this code:

export const runCommand = (args) => {

}

Now in my Node.js app, I'd like to some how download this remote file and import the runCommand function. I've tried looking into the VM Contexts however I was only successful at executing the code if it was called in that remote file.

In this situation, I would like to download the code, get the runCommand function, and then invoke it in my app whenever I want to. Is this possible?

  • Is the code only available within another code repo/web server or is it exposed as an npm package (check if there is a package.json file)? If so, you could just install it with npm and then import the required function. If it is not packaged, you could just copy the code and all of its imports.. You won't be able to download the package and then dynamically import its content on runtime with Node.js. You could try Deno instead of Node.js which uses this concept of imports. – ssc-hrep3 Oct 05 '21 at 15:33
  • @ssc-hrep3 The code would only be available via some remote server, like a regular file, no NPM package. I'm essentially trying to create "remote functions" that can be imported and executed during runtime. – Dogwrench Oct 05 '21 at 15:36
  • Maybe take a look at https://deno.land/manual@v1.14.3/examples/import_export#remote-import – ssc-hrep3 Oct 05 '21 at 15:38

1 Answers1

0

i am assuming you know the path to the remote command JS file you want to import. As such, I would reference this topic, and it should help with the URL-based file access:

ES6: import module from URL

if you don't know the path, then it's a different problem.

G-Force
  • 345
  • 3
  • 10