What language is a .env file written in?

17,711

Solution 1

Assuming you're referring to how the .env file is interpreted by the npm dotenv package:

The file simply serves as a 'text' configuration, which is parsed by the module. The parsed configuration is used as a basis for adding environment variables. So the env file itself is not really written in any programming language.

Parsing rules can be found here: https://www.npmjs.com/package/dotenv#rules

Parsing code can be found here: https://github.com/motdotla/dotenv/blob/master/lib/main.js

Solution 2

Here are the rules, copy-pasted from link above. Feel free to update anytime ("Commnity wiki").


Rules

The parsing engine currently supports the following rules:

  • BASIC=basic becomes {BASIC: 'basic'}

  • empty lines are skipped

  • lines beginning with # are treated as comments

  • empty values become empty strings (EMPTY= becomes {EMPTY: ''})

  • inner quotes are maintained (think JSON) (JSON={"foo": "bar"} becomes {JSON:"{\"foo\": \"bar\"}")

  • whitespace is removed from both ends of unquoted values (see more on trim) (FOO= some value becomes {FOO: 'some value'})

  • single and double quoted values are escaped (SINGLE_QUOTE='quoted' becomes {SINGLE_QUOTE: "quoted"})

  • single and double quoted values maintain whitespace from both ends (FOO=" some value " becomes {FOO: ' some value '})

  • double quoted values expand new lines, MULTILINE="new\nline" becomes

    {MULTILINE: 'new
    line'}
    
Share:
17,711

Related videos on Youtube

mareoraft
Author by

mareoraft

Interested in in Data Science, Go, and Python.

Updated on September 15, 2022

Comments

  • mareoraft
    mareoraft over 1 year

    By convention, what language or syntax is a .env file written in?

    Is it a sh script, a bash script, JavaScript, or is it a stripped down no-frills syntax inspired by sh syntax? Are all variables defined strings, or are other variable types supported?

    I recently started using .env files in my NodeJS applications, but I can find nowhere in the documentation what language it is in or what constraints on syntax I must follow. I found a definition in the docker docs which seems to suggest MY_VAR=my val is the only notable feature, per se.

    EDIT:

    While I encountered this in the context of NodeJS, the question is not specific to nodeJS. .env files used in other contexts should be considered.

    • Andrew Ault
      Andrew Ault almost 5 years
      if your question is specific to node.js, you may find the information you're looking for here: npmjs.com/package/dotenv#rules
    • Tharun208
      Tharun208 almost 5 years
      From my point of view, It is not a programming language and it is a file containing different environment variables which is usually a key/value pair.
  • mareoraft
    mareoraft almost 5 years
    Thanks, that is helpful. It grabs the key-value pairs with the regex /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/, and then the value is trimmed among other things. The value is always a string.
  • yzorg
    yzorg over 2 years
    This differs from the rules stated on VSCode Python .env documentation. I believe VSCode python extension is using npm dotenv or something equivalent, b/c it describes the double-quoting behavior I'm seeing. But docs say no quoting.