DEV Community

Anand Chowdhary
Anand Chowdhary

Posted on

Run a remote Node.js file quickly

If you have a local Node.js file that does something, it's very easy to run:

node path/to/file.js

However, if you have a remote file on a URL, say https://example.com/script.js, you have several steps before you can get your desired output in your terminal:

  1. Download script.js (using curl, for example) to a directory
  2. Create a package.json file to make sure it runs
  3. Check if it has any external dependencies
  4. Install external dependencies (using npm install, for example)
  5. Use node script.js to execute it

This is too much of work, especially if you want to get started quickly. So, introducing run-url: an open-source npm remote URL runner written in TypeScript.

Now, you can just run the URL using npx, just like you're used to:

npx run-url https://example.com/script.js

You can also install run-url globally:

npm install --global run-url

And it'll be available in your terminal:

run-url https://example.com/script.js

Running GitHub Gists with Node.js

If you quickly want to prototype, a great way is to create a GitHub Gist. For example, I have the following: https://gist.github.com/AnandChowdhary/3c0400b29a18a2afff7a23e2a3308c22#file-log-moment-js, which has a dependency (moment) and outputs the current date.

Simply copy the raw URL of the gist file and run it:

npx run-url https://gist.githubusercontent.com/AnandChowdhary/3c0400b29a18a2afff7a23e2a3308c22/raw/8ab3ca87821511e580d72585c72736b833d18697/log-moment.js

run-url will install the required dependency (in this case, moment) in a temporary folder, run the Node.js file, and then remove the temporary folder. Just like npx, it just works.

See the source on GitHub or Download from npm

Top comments (0)