1

I'm new to scripting and am trying to figure out if it is possible to run a script on the command line written in Javascript, just by typing "./script-name arg1 arg2". I want to use Javascript because arg2 is a JSON file, but am open to changing to another language if I need to in order to call the script in that exact way. Any ideas?

Edit: I've looked at the question here but none of the solutions run the script with "./script-name" like I want it to.

sherf
  • 33
  • 1
  • 1
  • 5
  • 1
    Possible duplicate of [How do you run JavaScript script through the Terminal?](https://stackoverflow.com/questions/8532960/how-do-you-run-javascript-script-through-the-terminal) – Endless Dec 25 '17 at 21:16
  • 1
    Do you mean "not using node at all" or just writing the command `node`? – A.D. Dec 25 '17 at 21:23
  • There is no reason arg2 can't be a JSON file and not use node. – Charlie Fish Dec 25 '17 at 21:31
  • @Endless, none of the solutions there avoid the issue of needing to install rhino/node. I want to be able to call the script by saying "./script-name arg1 arg2" exactly. – sherf Dec 25 '17 at 21:36
  • @A.D. Sorry for the vagueness- I meant that I don't want to write "node script-name,js" to run my script, I want to write "./script-name arg1 arg2". – sherf Dec 25 '17 at 21:37
  • 1
    If you want to write "./script-name" use node. What's your problem with installing node? – slebetman Dec 25 '17 at 21:38
  • @sherf It still doesn't make sense why you are trying to avoid node. – Charlie Fish Dec 25 '17 at 21:39
  • 1
    Also, what OS? I'm assuming, since you're asking this, that you use Windows? – slebetman Dec 25 '17 at 21:40
  • @sherf on of the solution there is for mac that comes pre installed with a cmd line tool – Endless Dec 26 '17 at 21:06

1 Answers1

11

Node.js honors the age-old unix tradition of the shbang line. Therefore all you need to do to make your node.js script behave like a regular executable program is to add this as the first line:

#! /usr/bin/env node

Technically this does not run node but rather it runs the env comand (available on all unixen) which then finds where node.js is installed and runs it. This allows your script to be portable accross multiple distros (ubuntu, redhat etc.) and accross different unixes (Linux, MacOS, BSD etc.).

Then you just need to make your script executable:

chmod +x your-script.js

On Windows however, it gets a bit more complicated. However, npm has built-in features to turn your script into a proper command so you don't even need to do:

./your-script.js

You can just do:

your-script

All you need to do is:

  1. Add the shbang line to your script.
  2. If on unix do the chmod +x thing.
  3. Turn your script into an npm project (if not already an npm project) by putting it into its own folder then running npm init inside that folder (this will create a package.json file)
  4. Edit your package.json file and add a bin property

    "bin" : {
        "your-script" : "./your-script.js"
    }
    
  5. Run npm link in the directory

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • How about without chmod ? – Andrey Izman Oct 31 '18 at 18:43
  • 1
    @AndreyIzman You only have two choices, make the script executable with chmod or run it with `node your-script`. This is true for all languages (bash, python, ruby etc). Even compiled binary that is not marked as executable (maybe you downloaded it from email or Whatsapp) needs to be `chmod` to work. Your compiler will automatically chmod it for you but you sometimes can lose the executable flag if you transfer you program through non-standard ways (email, Whatsapp etc.) – slebetman Nov 02 '18 at 01:39
  • where to write this command ```#! /usr/bin/env node``` beacuse in my root dir env file doen't exist at the above path – Holasmabre Jan 28 '21 at 05:15
  • @VishwajeetGade `env` is a command that is available on most unix OSes including Linux. To test if your OS has an `env` command type `/usr/bin/env` then press enter on the command line. Do this anywhere including outside your node folder. I think you should especially do it outside your node folder to make sure your `env` program is properly installed. Almost all modern Unix OS should have a properly configured `env` command just as they should all have a properly configured `ls` command. – slebetman Jan 28 '21 at 18:01
  • @VishwajeetGade The `#!` line is called a shbang line and should be the first line in your shell script. It is part of sh compatible scripting languages including bash, tcsh and ksh. Node.js supports the shbang line as well to allow unix shells to execute node scripts as if it is an executable. As such `#! /usr/bin/env node` should be the first line in your javascript file (only the main file - not modules) and not part of any other files so not part of your .env file (if you use dotenv), not part of your json file, not part of your config file and not part of other js files – slebetman Jan 28 '21 at 18:05