liveBook · Manning

.env file

As explained in Build Financial Software with Generative AI (From Scratch)

An .env file is a conventionally named environment file containing application configuration values as environment-variable assignments.

An .env file stores configuration as environment-variable assignments, keeping values such as credentials and service URLs out of application code. The filename is flexible: examples include .env, .env.local, and .sql_server.conf. Keep sensitive files out of source control, and document the required settings so others can configure new instances. section 3.7 section 6.5.3

Use the file to change configuration without editing hardcoded values:

  • For Docker, specify the environment file with the service’s env_file directive. section 3.7
  • For a Next.js API call, read the configured URL from process.env instead of embedding localhost in the component. section 6.5.3
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret

Example assignments for PostgreSQL credentials. section 3.7

  postgres:
    build: 
      context: ./db
      dockerfile: Dockerfile
    env_file:
      - ./.sql_server.conf

Docker service configuration that loads an environment file. section 3.7

Listing 6.27 Our .env.local file
NEXT_PUBLIC_API_URL=http://localhost:8081/api/v1

A Next.js local environment file defining the API base URL. section 6.5.3

Listing 6.28 Updated API call
    useEffect(() => {
        const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? ''; #1
        axios.get(`${apiUrl}/files`)       #2

Updated API call that reads its base URL from process.env. section 6.5.3

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Shows environment assignments and their use through Docker and Next.js, without detailing a broader configuration system.
Examples
Uses PostgreSQL credentials in Docker and a local API base URL in a Next.js application.
What you can do after
Configure Docker services and Next.js API calls using values stored outside application code.
How it connects
Connects environment variables with Docker, source control, Next.js, and API configuration.

Also explained in

Compare how 5 books explain .env file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.