argv and env

process.argv slice

The argv array always holds at least two entries: the Node path and the script path. User words start at index two, so slice there to isolate them.

const words: string[] = process.argv.slice(2); console.log(words.join(", "));

node main.js hello brave world

hello, brave, world

Parse simply

Small tools need only positional words, not a parser library. Read by position, convert numbers with Number, and fall back when conversion fails.

const words: string[] = process.argv.slice(2); const name: string = words[0] ?? "Ada"; const count: number = Number(words[1] ?? 1); if (Number.isNaN(count)) { console.log("Count must be numeric"); }

node main.js Ada abc

Count must be numeric

process.env

Environment values carry settings that change between machines, such as ports and modes. They arrive as strings or undefined, never as numbers.

const mode: string = process.env.MODE ?? "dev"; console.log("Mode:" + mode);

MODE=prod node main.js

Mode:prod

Defaults for missing

Always plan for missing input, because users forget flags and servers forget settings. Nullish coalescing keeps one clear default in one place.

const portText: string = process.env.PORT ?? "3000"; const port: number = Number(portText); const safePort: number = Number.isNaN(port) ? 3000 : port; console.log(safePort);

3000

Text default first, numeric conversion second, sanity check third. That order turns absent or broken settings into a safe local port.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. At which index do the user words start in process.argv?

    Answer

    Index 2, after the Node path and the script path

    The first two slots belong to the runtime and the script, so user input starts at two.

  2. What type does a value of process.env have?

    Answer

    string | undefined

    The environment holds text, and a variable that is not set is undefined.

  3. How is a missing environment value given a default?

    Answer

    With ??, so the fallback is used only for null or undefined

    The nullish operator keeps a real value and replaces only an absent one.

  4. What is the safe order for reading a numeric setting?

    Answer

    Text default, then Number, then a check for NaN

    The environment gives text, so a default comes first, conversion second, and the sanity check last.

  5. What does this program print? const words = ["hello", "brave", "world"]; console.log(words.join(", "));

    Answer

    hello, brave, world

    The join puts a comma and a space between the words.

  6. What does this program print? console.log(Number("abc"));

    Answer

    NaN

    Number returns NaN when the text cannot be read as a number.

  7. What does this program print? console.log(Number.isNaN(Number("abc")));

    Answer

    true

    The conversion gives NaN, and isNaN reports it as such.

  8. What does this program print? console.log(Number("42") + 1);

    Answer

    43

    Number turns the text into 42, and the plus adds one.

  9. What does the compiler say about this program? const port: number = process.env.PORT ?? 3000; console.log(port);

    Answer

    error TS2322: Type 'string | 3000' is not assignable to type 'number'.

    The environment value is a string, so the result is a string or the number 3000, not a number.

  10. What does the compiler say about this program? const mode: string = process.env.MODE; console.log(mode);

    Answer

    error TS2322: Type 'string | undefined' is not assignable to type 'string'.

    An unset variable is undefined, and strict mode refuses to store that in a string.

Article author: Arthur Isaev