Running Node.js Scripts ยท Astro Tech Blog

Three Ways to Run JavaScript with Node.js

MethodCommandUse Case
Script filenode app.jsMost common
REPLnodeQuick experiments
Evaluatenode -e "code"One-liners

1. Running a Script File

node app.js
node path/to/script.js
// app.js
console.log('Hello from a file!');

Watch Mode (Node.js 18+)

node --watch app.js

Restarts automatically when files change โ€” no need for nodemon.

2. The REPL (Read-Eval-Print Loop)

Launch it with just node:

$ node
Welcome to Node.js v22.x.x.
> 2 + 2
4
> const greeting = `Hello, ${process.env.USER}`;
undefined
> greeting
'Hello, abhishek'
> .exit

Useful REPL Commands

CommandAction
.helpShow all commands
.breakExit multi-line input
.clearReset context
.save file.jsSave session to file
.load file.jsLoad file into session
.editorMulti-line editor mode
.exitQuit

REPL One-liner with -e

node -e "console.log(2 ** 10)"   # 1024
node -e "console.log(new Date().getFullYear())"
node --eval "const os = require('os'); console.log(os.cpus().length)"

3. Command-Line Arguments

Access arguments via process.argv:

// greet.js
const args = process.argv.slice(2);
const name = args[0] || 'World';
console.log(`Hello, ${name}!`);
node greet.js Abhishek
# Hello, Abhishek!

node greet.js
# Hello, World!

process.argv[0] is the node binary path, process.argv[1] is the script path. slice(2) gets your actual arguments.

Parsing Flags

// args.js
const args = process.argv.slice(2);
const flags = {};

for (let i = 0; i < args.length; i++) {
  if (args[i].startsWith('--')) {
    const key = args[i].slice(2);
    const value = args[i + 1] && !args[i + 1].startsWith('--') ? args[i + 1] : true;
    flags[key] = value;
    if (value !== true) i++;
  }
}

console.log(flags);
node args.js --name Abhishek --verbose
# { name: 'Abhishek', verbose: true }

4. Environment Variables

// env.js
console.log('Home:', process.env.HOME);
console.log('Node env:', process.env.NODE_ENV);
console.log('Custom:', process.env.MY_VAR);
MY_VAR=hello node env.js
# Home: /home/abhishek
# Node env: undefined
# Custom: hello

Pro tip: Use a .env file with the dotenv package for managing environment variables in development.

Accessing process.env

const config = {
  port: parseInt(process.env.PORT, 10) || 3000,
  host: process.env.HOST || '0.0.0.0',
  debug: process.env.DEBUG === 'true',
  db: {
    url: process.env.DATABASE_URL,
    pool: parseInt(process.env.DB_POOL, 10) || 10,
  },
};

5. Shebang Scripts (CLI Executables)

Make Node.js scripts executable like system commands:

#!/usr/bin/env node
// greet-x.js
const name = process.argv[2] || 'World';
console.log(`Hello, ${name}!`);
chmod +x greet-x.js
./greet-x.js Abhishek
# Hello, Abhishek!

6. Exit Codes

// exit.js
const args = process.argv.slice(2);

if (args.length === 0) {
  console.error('Error: No arguments provided');
  process.exit(1);  // Non-zero = failure
}

console.log('Arguments:', args);
process.exit(0);  // Zero = success
node exit.js && echo "Success"
# Arguments: []
# Error: No arguments provided

node exit.js hello && echo "Success"
# Arguments: [ 'hello' ]
# Success

Putting It All Together

#!/usr/bin/env node
// cli-tool.js

const name = process.argv[2] || process.env.USER || 'World';
const debug = process.argv.includes('--debug');

if (debug) {
  console.log('Args:', process.argv);
  console.log('Env:', process.env.NODE_ENV);
  console.log('CWD:', process.cwd());
}

console.log(`Hello, ${name}!`);
node cli-tool.js Alice --debug
# Args: [ '/usr/bin/node', '/path/to/cli-tool.js', 'Alice', '--debug' ]
# Env: undefined
# CWD: /path/to
# Hello, Alice!

Key Takeaways

  • Use node script.js to run files, node for the REPL
  • Access CLI arguments via process.argv
  • Read environment variables from process.env
  • Shebang (#!/usr/bin/env node) turns scripts into executables
  • process.exit(code) controls the exit status (0 = success, non-zero = error)