@env-spec/parser

中文文档
0.6.3发布于 2026/11/13MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4.7万

Parser for @env-spec enabled dotenv files

原始使用文档
文档目录16 节
  1. How can schema data be used?
  2. Backwards compatibility
  3. What is this package?
  4. Comments
  5. Comment
  6. DecoratorComment
  7. Divider
  8. CommentBlock
  9. DocumentHeader
  10. Decorators
  11. Config Items
  12. Line endings
  13. Common Value-Handling Rules
  14. Function calls
  15. Notable differences with dotenv/dotenv-expand/dotenvx
  16. Local dev and testing workflow

@env-spec

We'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:

  • add structured metadata using @decorator style comments similar to JSDoc
  • formalizes a syntax for setting values via function calls

Here 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:

  • additional validation, coercion, type-safety on your env vars
  • extra guard-rails around handling @sensitive data
  • more flexible loading logic without additional application code or config files

How can schema data be used?

This 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 committed 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 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.

Backwards compatibility

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.

What is this package?

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


Language Syntax Reference

This is a reference of the details of the env-spec language itself. Here we don't make any assumptions about the meaning of specific decorators, or function calls.

Comments

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 comment
  • KEY=val # so is this
  • # but this is invalid

We give these comments additional meaning by letting them contain @decorators and attaching them to specific config items.

Comment

A regular comment is a comment that does not start with @

  • Leading whitespace is optional -- #these, # are, # all valid
  • Decorators within are ignored -- # this @decorator is ignored

DecoratorComment

A decorator comment is a comment that starts with an @ and contains decorators

  • It may contain one or multiple decorators -- # @type=integer, # @sensitive @required
  • There may be an additional regular comment after the decorator(s) -- # @sensitive=false # key is published in final build

Divider

A divider is a comment that serves as a separator, like a horizontal line

  • A comment starting with --- or === is considered a divider -- # ---, # ===
  • A single leading whitespace is optional -- # ---, #---
  • Anything after that is ignored and valid -- # --- some info, # ------------

CommentBlock

A comment block is a group of continuous comments that is not attached to a specific config item.

  • The comment block is ended by an empty line, a Divider, or the end of the file.
  • Both DecoratorComments and RegularComments may be interspersed

DocumentHeader

If a CommentBlock ends with a Divider and is the first element of the document, it will be considered the Header.

  • Decorators from this header can be used to configure all contained elements, or the loading process itself

Decorators

A Decorator is used within comments to attach structured data to specific config items or to the entire document and loading process.

  • Each decorator has a name and optional value -- @name=value
  • Using the name only is equivalent to setting the value to true -- @required === @required=true
  • Decorator values will be parsed using the common value-handling rules (see below)

Valid 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

Config items define individual env vars. Each has a key, an optional value, and optional attached comments.

  • Keys must start with [a-ZA-Z_], followed by any of [a-ZA-Z0-9_] -- ✅ SOME_ITEM, ❌ BAD-KEY, ❌ 2BAD_KEY
  • Setting no value is allowed and will be treated as undefined -- UNDEF_VAR=
  • An empty string is allowed -- EMPTY_STRING_VAR=""
  • Single-line values may be wrapped in quotes or not, and will follow the common value-handling rules (see below)
  • Multi-line values may be wrapped in either ( " | """ | ``` )
  • Only comments directly preceding the item will be attached to the item
  • However a Divider will break the above comments into a CommentBlock that is not attached to the item
  • An additional post-comment can appear after the item ITEM1=foo # post comment
  • This post-comment can contain decorators ITEM1=foo # @required

Line endings

Files may use \n or \r\n line endings. Both are treated the same, so parsed values are identical either way. Node and syntax error locations (offset, line, column) always refer to the text you passed in, including any \r characters.

Common Value-Handling Rules

Values 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.

  • Values may never contain actual newlines, but may contain the string \n
  • Values do not have to be wrapped in quotes if they do not contain spaces
  • Unquoted values also may not contain other characters depending on the context:
    • ConfigItem values may not contain [ #]
    • Decorator values may not contain [ #]
    • FunctionCall args may not contain [ ,)]
  • Unquoted values will coerce true, false, undefined -- @foo=false
  • Unquoted values will coerce numeric values -- @int=123 @float=123.456
  • Otherwise unquoted values will be treated as a string
  • A value in quotes is always treated as a string -- @d1="with spaces" @trueString="true", @numStr="123"
  • All quote styles [`'"] are ok -- @dq="c" @bt=`b` @sq='a'
  • Escaped quotes matching the wrapping quote style are ok -- @ok="escaped\"quote"
  • In quote-wrapped values, the string \n will be converted to an actual newline

Function calls

If 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.

  • function names must start with a letter, and can then contain letters, numbers, and underscores /[a-ZA-Z][a-ZA-Z0-9_]*/
  • function args are always interpreted as either an array or object
  • you can pass no args, a single value, or multiple
  • or you may pass key value pairs, and the args will be interpreted as an object
  • each value will be interpreted using common value-handling rules (see above)

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)

Notable differences with dotenv/dotenv-expand/dotenvx

  • we do not support nested default expansion (ex: VAR=${FOO:-${BAR}}
    • instead you can use the fallback function directly VAR=fallback(ref(FOO), ref(BAR))
  • we do not support unescaped quotes within exec expansion (ex: VAR="$(echo "foo")")
    • we support backtick quotes, escaped quotes, or you can use exec function directly VAR=exec(echo "foo")
  • $(...) expansion supports nested parentheses and quoted ) (ex: $(jq -r '.address|join(".")'))

Local dev and testing workflow

  • bun run dev - builds and watches everything
  • bun run test builds everything, run tests, and watches for changes to re-run
  • bun run test:ci will just build and run the tests once

If 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.

版本与发布记录

@env-spec/parser 共发布 28 个版本,首次发布于 2025/05/05;过去 12 个月发布了 20 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.3latest2026/11/13今天
0.6.22026/10/1430 天前
0.6.12026/09/261 个月前
0.6.02026/09/191 个月前
0.5.22026/09/122 个月前
0.5.12026/08/252 个月前

关于 @env-spec/parser 的常见问题

@env-spec/parser 是什么?

Parser for @env-spec enabled dotenv files

如何安装 @env-spec/parser?

在项目目录运行 npm install @env-spec/parser,或使用 pnpm add / yarn add / bun add。

@env-spec/parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@env-spec/parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@env-spec/parser 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@env-spec/parser 可以商用吗?

@env-spec/parser 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@env-spec/parser 的下载量有多少?

根据 npm 官方下载统计,@env-spec/parser 在 2026/10/14 – 2026/11/12 这 30 天内被下载 46,815 次,日均约 1,561 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@env-spec/parser 还在维护吗?

最新版本 0.6.3 发布于 2026/11/13(今天);至今共发布 28 个版本,其中过去 12 个月发布了 20 个;首次发布于 2025/05/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。