How to Access Environment Variables in a Makefile


How can we access environment variables inside our Makefile?

Suppose this is our project structure.

📂 project
 ┣ 📜 Makefile
 ┗ 📜 .env

Unfortunately, Makefile doesn’t automatically have access to the root .env file, which might look something like this.

# .env
HOSTNAME=localhost
PORT=3000

That said, we can include the appropriate environment file in our Makefile.

# Makefile
include .env
URI:=$(HOSTNAME):$(PORT)

The environment variables are then accessible using the $(VAR_NAME) syntax.