Caretaker Express · PostgreSQL · Cedar

Module 0 · Orientation and environment setup

0.3 Terminal, folders and environment variables

Create the empty project folder, learn the few terminal ideas the course relies on, and see how environment variables reach a Node.js process.

≈ 20 min 8 steps 0 lines of code Replay verified

Starting point

Your tools work (lesson 0.2). You do not have a project folder yet.

Outcome

You will have an empty, Git-tracked folder called caretaker, and you will understand:

  • the working directory, and why "run this from the project folder" matters;
  • environment variables, and how a Node.js process reads them through process.env;
  • why secrets go into environment variables and not into code.
Text
~/code/└── caretaker/        ← empty, with a .git folder

The problem

Two facts trip people up in every Node.js project:

  1. Commands such as npm install act on the current folder. Run them one folder too high and you create a stray package.json in your home directory.
  2. Configuration such as the database password must reach the program without being written into the source code. Environment variables are the standard way to do that, and Node.js reads them in a specific way.

Build it

1. Make a home for your code

Pick one place for course code. We use ~/code. ~ means your home folder (/Users/<you> on macOS).

Example command
mkdir -p ~/code
cd ~/code
  • mkdir -p creates the folder and does nothing if it already exists.
  • cd changes the working directory of your terminal.

2. Create the project folder

From ~/code, create the folder for Caretaker:

Runcaretaker/
mkdir caretaker

Now move into it. From this point on, every command in the course runs from inside caretaker unless a lesson says otherwise.

Example command
cd caretaker

Check where you are with pwd ("print working directory"):

Run and comparecaretaker/
pwd
/Users/you/code/caretaker

3. Start a Git repository

Run and comparecaretaker/
git init
Initialized empty Git repository in /Users/you/code/caretaker/.git/

Git created a hidden .git folder. That folder is the repository: it stores every commit. ls -a shows hidden files:

Run and comparecaretaker/
ls -a
.
..
.git

. is the current folder and .. is its parent. We commit at the end of most lessons, so you can always compare or go back.

4. Environment variables

Every process gets a set of key/value strings from the process that started it. Your terminal has many already:

Run and comparecaretaker/
echo "$HOME"
/Users/you

You can set a variable for one command by writing it before the command. Node.js exposes environment variables in the object process.env:

Run and comparecaretaker/
GREETING="Hello from the environment" node -e 'console.log(process.env.GREETING)'
Hello from the environment

node -e runs the JavaScript in quotes. The variable existed only for that one node process. Run the same command without the prefix:

Run and comparecaretaker/
node -e 'console.log(process.env.GREETING)'
undefined

Two rules follow, and we rely on both later:

  • Values in process.env are always strings (or undefined). PORT=3000 gives you "3000", not the number 3000.
  • A missing variable is undefined, not an error. If our app needs a value, our code must check for it and stop with a clear message.

5. Why configuration goes into the environment

In Module 2 our app needs a database URL that contains a password. In Module 4 it needs a session secret. We will keep them in a file named .env, load that file with Node's --env-file option, and tell Git to ignore .env. The code then reads process.env.DATABASE_URL and never contains the secret itself. A teammate, a test run and a production server can each supply different values without changing code.

Run and inspect

Confirm that the folder is empty apart from .git, and that Git agrees:

Run and comparecaretaker/
git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Experiment

Predict the output before you run it:

Example command
PORT=3000 node -e 'console.log(typeof process.env.PORT, process.env.PORT + 1)'
Reveal Answer

It prints string 30001. The value is the string "3000", and + 1 joins strings. Our configuration code in Module 2 converts numeric settings with Number(...) for exactly this reason.

Checkpoint

  • pwd prints a path that ends in /caretaker.
  • git status says "No commits yet".
  • You can explain why process.env.PORT + 1 is not 3001.
Symptom fatal: not a git repository later in the course

You ran a Git command outside the project folder. Run pwd, then cd ~/code/caretaker.

Symptom You created package.json in the wrong folder

If you run npm init from ~/code by mistake, delete the stray ~/code/package.json and run the command again from inside caretaker.

Recap and next step

You have an empty Git repository in ~/code/caretaker, and you know that commands act on the working directory and that environment variables arrive in process.env as strings.

In the next module we turn this folder into a Node.js project and start an Express server.