
Company News
Free Business Plan Upgrades for Open Source Maintainers
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.
@env-spec/parser
Advanced tools
@env-specWe're actively seeking feedback on @env-spec. Please share your thoughts and ideas in the RFC.
@env-spec is a simple DSL/language that extends normal dotenv syntax, allowing you to:
@decorator style comments similar to JSDocHere is a short illustrative example:
# Stripe secret api key
# @required @sensitive @type=string(startsWith="sk_")
# @docs(https://docs.stripe.com/keys)
STRIPE_SECRET_KEY=fetchSecret("stripe/secret-key")
This structured data can be used by libraries to provide:
@sensitive dataThis schema information is most valuable when it is shared across team members and machines - so it is intended to be used within a file which is comitted to git.
In most cases, that will mean creating a .env.schema, committed to source control, which contains all schema info and possibly some default values.
It's not very different than a having a .env.example file - it's just more useful and actually involving it in the env loading process.
Then you could use additional files which set values - and of course they could add additional items or overriding properties of existing ones.
Whether you want to use a single git-ignored .env file, or apply a cascade of environment-specific files (e.g., .env, .env.local, .env.test, etc) is up to you.
However the new ability to use function calls to safely decrypt data, or load values from external sources, means you'll likely be tempted to use committed .env files much more.
An env-spec enabled tool would load all env files appropriately, merging together both schema and values, as well as additional values read from the shell/process. Then the schema would be applied which could transform/fill values, for example decrypting or fetching from an external source, as well as applying coercion and validation.
| In a very simple project, you can also imagine using a single committed .env file which contains both schema and values, and takes advantage of function calls to securely load sensitive info.
This is designed to be mostly backwards compatible with traditional .env files, however there is no standard .env spec and various tools have slightly different rules and features, so we make some decisions to try to standardize things. Tools may support additional compatibility flags if users want to opt in/out of specific behaviours that match other tools.
The extended feature set means an env-spec enabled parser will successfully parse env files that other tools may not.
This package defines a parser and related tools for parsing an @env-spec enabled .env file.
It does not provide anything past this parsing step - like actually loading environment variables.
For a usable tool which lets you actually use it in your .env files, check out https://varlock.com
This is a reference of the details of the env-spec language itself. Here we don't make and assumptions about the meaning of specific decorators, or function calls.
Comments in env-spec (like dotenv) start with a #. Comments can be either on their own line, or at the end of a line after something else.
# this is a commentKEY=val # so is this # but this is invalidWe give these comments additional meaning by letting them contain @decorators and attaching them to specific config items.
CommentA regular comment is a comment that does not start with @
#these, # are, # all valid # this @decorator is ignoredDecoratorCommentA decorator comment is a comment that starts with an @ and contains decorators
# @type=integer, # @sensitive @required# @sensitive=false # key is published in final buildDividerA divider is a comment that serves as a separator, like a horizontal line
--- or === is considered a divider -- # ---, # ===# ---, #---# --- some info, # ------------CommentBlockA comment block is a group of continuous comments that is not attached to a specific config item.
Divider, or the end of the file.DecoratorComments and RegularComments may be interpersedDocumentHeaderIf a CommentBlock ends with a Divider and is the first element of the document, it will be considered the Header.
A Decorator is used within comments to attach structured data to specific config items or to the entire document and loading process.
@name=value@required === @required=trueValid decorator examples:
# @willBeTrue @willBeFalse=false @explicitTrue=true @undef=undefined
# @int=123 @float=123.456 @willBeString=123.456.789
# @quoted="with spaces" @trueString="true"
# @singleQuote='hi' @backTickQuote=`hi`
# @withNewline="new\nline"`
# @funcCallNoArgs=func() @dec=funcCallArray(val1, "val2") @dec=funcCallObj(k1=v1, k2="v2")
Invalid decorator examples:
# @
# @int=
# @spaceNeedsQuotes=spaces without quotes
# @noNewLines="new
# laksdjf"
Config items define individual env vars. Each has a key, an optional value, and optional attached comments.
SOME_ITEM, ❌ BAD-KEY, ❌ 2BAD_KEYundefined -- UNDEF_VAR=EMPTY_STRING_VAR=""( " | """ | ``` )Divider will break the above comments into a CommentBlock that is not attached to the itemITEM1=foo # post commentITEM1=foo # @requiredValues are interpreted similarly for config item values, decorator values, and values within function call arguments. Values may be wrapped in quotes or not, but handling varies slightly.
\nConfigItem values may not contain [ #]Decorator values may not contain [ #]FunctionCall args may not contain [ ,)]true, false, undefined -- @foo=false@int=123 @float=123.456@d1="with spaces" @trueString="true", @numStr="123"[`'"] are ok -- @dq="c" @bt=`b` @sq='a'@ok="escaped\"quote"\n will be converted to an actual newlineIf a value is not wrapped in quotes and looks like a function call - for example varlock(ASDF123...) - we will interpret it as a FunctionCall. This is relevant both for config item values and decorator values.
/[a-ZA-Z][a-ZA-Z0-9_]*/Examples:
# @noArgs=fn()
# @oneArg=fn(asdf) @oneArgQuoted=fn("with quotes")
# @multipleArgs=fn(one, "two", three, 123.456)
# @objArgs=fn(key1=v1, key2="v2", key3=true)
VAR=${FOO:-${BAR}}
fallback function directly VAR=fallback(ref(FOO), ref(BAR))VAR="$(echo "foo")")
exec function directly VAR=exec(echo "foo")$(...) expansion supports nested parentheses and quoted ) (ex: $(jq -r '.address|join(".")'))bun run dev - builds and watches everythingbun run test builds everything, run tests, and watches for changes to re-runbun run test:ci will just build and run the tests onceIf you need to pass extra flags to vitest (for example to run specific tests/files)
run bun run dev:grammar in one terminal and bunx vitest ... in another
Setting PEGGY_TRACE=1 will enable tracing in the built grammar file.
FAQs
Parser for @env-spec enabled dotenv files
The npm package @env-spec/parser receives a total of 3,097 weekly downloads. As such, @env-spec/parser popularity was classified as popular.
We found that @env-spec/parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.

Security News
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.

Security News
During a UK cyber test, a Mythos 5 agent used sockpuppets, social engineering, and prompt injection to try to get a maintainer to merge malware.