-1

Is it possible to run a js file locally that downloads a secondary file and then run the latter?

I know the best thing would be to download the file from the server and save it locally, I would just like to understand if there is still the possibility to do the above.

Example:

Open terminal

1- node index js

2 - download scripts js in the same directory as index.js

3 - download is complete, start script js automatically.

carver14
  • 41
  • 9
  • 1
    Does this answer your question? [how to require from URL in Node.js](https://stackoverflow.com/questions/7809397/how-to-require-from-url-in-node-js) – Krzysztof Krzeszewski Jun 28 '20 at 08:56
  • 1
    Node.js does not contain a built-in feature to directly load a module over the network. It loads modules from files. So you would need to write your own code that fetches the code over the network and then writes it to a file and calls `require()` on that temporary file or loads it into memory and executes it with `eval()` or with the `Function` constructor. If you publish your module as an NPM module, they could use NPM to fetch and install your module. – jfriend00 Jun 28 '20 at 09:40
  • hi, thanks for your help, i'm not sure i understand. I edited my post, is it possible to download a js locally with a function and then launch the new file? – carver14 Jun 28 '20 at 11:03
  • It's unclear what your goal is. Do you just want different computers to be able to use the same functionality? If so, you should make a simple API server with Node. If not and you want to run a script locally, there are better ways to handle this, like putting the script in a git repository. – Nick McCurdy Jun 28 '20 at 11:33
  • I would like to run a js file locally that downloads another js file to the computer. once the download of the secondary js file is complete, I would like it to be run automatically in the terminal without the user having to write the command line to launch the secondary js file. – carver14 Jun 28 '20 at 11:43
  • I am pretty sure it is [git](https://git-scm.com/) (also mentioned by Nick McCurdy) what you need, and what should you and your team use to achieve the desired result. – theDavidBarton Jun 28 '20 at 11:48
  • hi, @theDavidBarton, :) I edited my post to be as clear as possible. what i would like to do is download a js file and run it after downloading it without starting it with a new command line. I don't know how to do it, do you have an example to do this? Thanks for all – carver14 Jun 28 '20 at 12:22

1 Answers1

0

Even if it is possible to solve it with Node.js workarounds (like requireing your puppeteer script from an url etc.) I strongly suggest to use Git with your team to share the latest version of your script. Version control is a must in modern software development, especially if you are working in a team. By learning the 6-7 most basic git commands you can solve almost everything you need for the current use case.

There are many good tutorials how to get started with git, I recommend GitHub's https://try.github.io.

Of course: you need to convince your team to embrace the change, or in better case they are already using it for version control, so it will be familiar to them.

You will need to:

  1. Install Git locally;
  2. Create a new repository on a server where you can store the latest stable versions of all your scripts (if it is not an open source project make sure it is private! GitLab, GitHub and Bitbucket provides/hosts free private repositories that will suit your needs);
  3. Share the HTTPS (or SSH) URL of the repository with your team (e.g.: https://github.com/.../puppeteer-scripts.git) which they can clone;
  4. Make sure your changes are pushed to the remote git repository;
  5. Your team can pull the latest changes to their local computer (it will be their responsiblity to check if there were changes in the scripts, but you can notify them as well - there are automatic notifications if one starts to "watch" a git repository);

Lastly

Git can be frightening at first sight, but it can be learned in a few days and it makes code sharing smooth within your team.

One of Git's tagline describes very well why it will solve your problem:

--everything-is-local

theDavidBarton
  • 7,643
  • 4
  • 24
  • 51