← Blog

NodeJS: How To Run Scripts From The Terminal & Use Arguments

August, 2019


So we installed NodeJS on our machine.

Now we want to write a simple script, run it from the terminal & use some commandline arguments.

Write a simple script

  • Open your terminal
  • Create a file named index.js:
touch index.js
  • Add console.log('Hello') into it:
echo "console.log('Hello')" > index.js

Run it from the terminal

  • Run it:
node index.js

Use commandline arguments

  • Update index.js to use the commandline arguments and print them:
echo "const args = process.argv" > index.js
echo "console.log(args)" >> index.js
  • Run it with an argument:
node index.js miku86
  • We are seeing an array with 3 elements:

args[0] is the path to the executable file, args[1] is the path to the executed file, args[2] is the additional commandline argument from step 2.

So if we want to use our additional commandline argument, we can use it like this in a JavaScript file:


Further Reading


  • Do you use the native process or some libraries like yargs? Why?


Hi! I'm Michael 👋 I'm a problem solver and mentor. My goal is to build great products and help you to become a better developer.