Execute node app from anywhere cover image

Execute node app from anywhere

Valentin Tsanev • April 12, 2022


node

Create binary

Use this package to create binary executable files:

https://github.com/vercel/pkg#readme

Pack for macos:

pkg app.js --target=macos --output

Add on first line to make executable:

#!/bin/bash

If use node without packaging:

#!/usr/bin/env node

To make executable from anywhere (on macos, with zsh):

  1. Create folder if doesn't exists and copy to:

    /Users/_USERNAME_/bin

  2. Change rights on a file:

    chmod 777 ~/bin/_FILE_APP_NAME_

  3. Add PATH env variable in bash_profil or .zshrc config:

    export PATH="/Users/_USERNAME_/bin:${PATH}"

Execute file if not added to bin

If not in bin execute file from folder where the file is:

./_FILE_APP_NAME_