---
title: "TIL: Node --env-file Replaces dotenv"
description: Since Node 20.6 the node --env-file flag reads a .env file into process.env before your code starts. No dotenv install and no require line in the entry file.
canonical: https://lukeocodes.dev/til-node-env-file
type: article
locale: en_GB
published: 2026-10-06T00:00:00.000Z
modified: 2026-10-06T00:00:00.000Z
authors:
  - Luke Oliff
section: TIL
tags:
  - TIL
  - JavaScript
  - nodejs
  - dotenv
  - environment variables
  - cli
feed: https://lukeocodes.dev/rss.xml
search: https://lukeocodes.dev/search.json?q={query}
schema:
  - "@context": https://schema.org
    "@graph":
      - "@type": WebSite
        "@id": https://lukeocodes.dev/#website
        url: https://lukeocodes.dev
        name: Luke Oliff
        alternateName: lukeocodes
        inLanguage: en
        publisher:
          "@id": https://lukeocodes.dev/authors/luke#person
        description: Personal blog and writing by Luke Oliff — developer relations, voice AI, developer experience.
        potentialAction:
          - "@type": SearchAction
            target:
              "@type": EntryPoint
              urlTemplate: https://lukeocodes.dev/blog/search?q={search_term_string}
            query-input: required name=search_term_string
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Engineer and developer relations leader. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Engineer and developer relations leader
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
  - "@context": https://schema.org
    "@graph":
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Engineer and developer relations leader. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Engineer and developer relations leader
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
      - "@type": BlogPosting
        "@id": https://lukeocodes.dev/til-node-env-file#article
        headline: "TIL: Node --env-file Replaces dotenv"
        alternativeHeadline: "TIL: Node --env-file Loads .env Without the dotenv Package"
        description: Since Node 20.6 the node --env-file flag reads a .env file into process.env before your code starts. No dotenv install and no require line in the entry file.
        datePublished: 2026-10-06T00:00:00.000Z
        dateModified: 2026-10-06T00:00:00.000Z
        url: https://lukeocodes.dev/til-node-env-file
        mainEntityOfPage: https://lukeocodes.dev/til-node-env-file
        inLanguage: en
        author:
          - "@id": https://lukeocodes.dev/authors/luke#person
        isPartOf:
          "@id": https://lukeocodes.dev/#website
        keywords: TIL, JavaScript, nodejs, dotenv, environment variables, cli
        articleSection: TIL
        abstract: Run node --env-file=.env app.js and Node reads the file into process.env before your code runs. Added in Node 20.6.0. A variable already set in the shell wins over the file, and a later --env-file flag wins over an earlier one.
        speakable:
          "@type": SpeakableSpecification
          cssSelector:
            - .post-tldr
            - h1
        image: https://lukeocodes.dev/og.png?h=TIL%3A+Node+--env-file+Replaces+dotenv&d=Since+Node+20.6+the+node+--env-file+flag+reads+a+.env+file+into+process.env+before+your+code+starts.+No+dotenv+install+and+no+require+line+in+the+entry+file.&e=Writing
        wordCount: 192
        timeRequired: PT1M
        publisher:
          "@id": https://lukeocodes.dev/authors/luke#person
      - "@type": BreadcrumbList
        itemListElement:
          - "@type": ListItem
            position: 1
            name: Home
            item: https://lukeocodes.dev
          - "@type": ListItem
            position: 2
            name: Writing
            item: https://lukeocodes.dev/blog
          - "@type": ListItem
            position: 3
            name: "TIL: Node --env-file Replaces dotenv"
            item: https://lukeocodes.dev/til-node-env-file
---

[Luke Oliff.](/)

- [Blog](/blog)
- [CV](/cv)
- [Uses](/uses)
- [User manual](/user-manual)
- [Living with ADHD](/living-and-working-with-adhd)
- [RSS](/rss.xml)

[Luke Oliff · Writing](/blog)

# TIL: Node --env-file Loads .env Without the dotenv Package

Oct 2026 · [TIL](/blog/category/til) · 1 min read · [Luke Oliff](/authors/luke)

**TL;DR:** Run node --env-file=.env app.js and Node reads the file into process.env before your code runs. Added in Node 20.6.0. A variable already set in the shell wins over the file, and a later --env-file flag wins over an earlier one.

Node can read a `.env` file on its own now. The flag is `--env-file`, and it has been in Node since 20.6.0.

```bash
node --env-file=.env app.js
```

Node reads the file before your code runs and puts each line into `process.env`. Comments, quotes and values that span more than one line work the same way they do in the dotenv package. If a variable is already set in your shell, the shell value wins. Pass the flag twice and the second file wins over the first, so `--env-file=.env --env-file=.env.local` gives you a shared file and a local override.

I wrote about [the dotenv package](https://lukeocodes.dev/til-dotenv-nodejs) back in 2020. For most projects you can uninstall it and delete the require line.

### What if the file is missing?

`--env-file` stops with an error when the file is not there. `--env-file-if-exists`, added in 22.9.0, carries on without it. Use that one in a Dockerfile where the `.env` only exists on your laptop.

### Can I load it from code instead?

Yes. `process.loadEnvFile(".env")` does the same thing from inside a script, and the argument is optional when the file is called `.env`. It landed in 20.12.0.

[← Newer: Does llms.txt Work? What a Year of Server Logs Says](/does-llms-txt-work)

[All posts](/blog) →

## Search this site

`GET https://lukeocodes.dev/search.json?q=<words>` returns matching posts as JSON, each with its page URL and its markdown URL. Add `&limit=<1-50>` for more than 10. How it works: [/blog/search](https://lukeocodes.dev/blog/search.md).

```sh
curl "https://lukeocodes.dev/search.json?q=developer+relations"
```

---

© 2026 Luke Oliff. Posts live at the root of this site.

- [GitHub](https://github.com/lukeocodes)
- [LinkedIn](https://www.linkedin.com/in/lukeocodes)
- [X](https://x.com/lukeocodes)
- [Bluesky](https://bsky.app/profile/lukeocodes.dev)
- [Email](mailto:luke@lukeoliff.com)
- [RSS](/rss.xml)

## Cookies

This site sets one analytics cookie, from Google Analytics, and only in a browser that accepts the cookie banner. A client that does not run scripts sets no cookies, so there is nothing to accept or decline here.

[Cookie policy](/cookies)
