Tag logo

Execute remote Node.js file

Jul 22, 2024/
#nodejs
/-1 min

In professional life, especially if you use microfrontend architecture, you need to manage a lot of projects. You keep scripts common to all these projects in one project and you may need to run these remote files in the pipeline of all projects.

If it has no dependencies, we can download it to the filesystem with

wget
or
curl
and run it.

1mkdir -p ./temp
2wget -O ./temp/$FILE_NAME.js "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.${HOST_NAME}.com/${FILE_NAME}.js"
3node ./temp/$FILE_NAME.js
4rm -rf ./temp

If there are dependencies you need to install them. The

npx run-url
tool downloads the file, installs the dependencies specified with
require
and then runs it. Then it cleans up everything.

1npx run-url "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.${HOST_NAME}.com/${FILE_NAME}.js"

Notes

  • The reason we add a token to the GitLab link is that the project is private. So you need to add a token to the URL to access it. Otherwise it returns the HTML of the login page.
  • You cannot use
    npx run-url
    if there is a Git command in the script. Using the first method, make sure you download the script to the same directory as
    .git
    .