Varlock Environment Validation for Node.js Startup and Logs
Use Varlock environment validation to block bad Node.js startup settings, test a synthetic token on stdout and stderr, and review the logs before sharing.
A Node.js test fails on a teammate’s machine, and you want to give a coding agent enough output to diagnose the failure. First find out whether the application started and whether the captured logs contain a token. This Varlock environment validation tutorial gives you a small fixture for both checks. You’ll run the same Node program with valid settings, a missing token, and an invalid environment name. Each attempt produces separate stdout, stderr, and exit status files that you can verify locally.
Use a POSIX-style shell such as Bash, with Node.js and the Varlock CLI already available on PATH. Work in a fresh subdirectory of a scratch copy of your repository. The token in the example is synthetic and has no corresponding account or service. The sample program makes no requests and loads no project dependencies. The outputs and verdicts shown here are illustrative expectations. Your own captures are the evidence for your run.
Cloudflare announced on December 2, 2026, that Varlock was joining the company. Its announcement shows how a .env.schema can declare requirements, types, and sensitivity alongside variable names. The fixture applies those declarations to a local launch and checks the two places where its sample token would otherwise appear. Cloudflare’s announcement

Define the two inputs in a scratch directory
- In the scratch copy, run
mkdir varlock-log-fixtureand thencd varlock-log-fixture. If the directory already exists, use a fresh scratch copy or a new directory so earlier captures and dotenv files cannot affect this exercise. Keep the repository’s existing configuration files in their original locations. - Save the first block as
.env.schema. Keep the divider above the first item and each decorator directly adjacent to the assignment it describes. The divider ends the header. A blank line between a decorator and its assignment would detach the comment. Varlock schema guide - Save the second block as
.env.local. Use the literal synthetic value supplied here. The expected output uses its lowercase prefix, and the checker searches for the entire original string, including the final digits.
# Local startup and log fixture
# ---
# @required @type=enum(dev, test, staging, prod)
APP_ENV=
# @required @sensitive @type=string
STARTUP_TOKEN=
APP_ENV=test
STARTUP_TOKEN=fixture-startup-token-9472
The schema leaves both assignments without a value. That makes .env.local the source of the values in the clean baseline. A valueless assignment resolves to undefined when there is no other source. @required fails an item that resolves to undefined or an empty string. This matters when you remove the token from .env.local. The declaration still exists, so you are testing whether Varlock enforces the requirement for that item. @sensitive identifies the string as a value to protect, and @type=string makes the intended representation explicit. Varlock schema guide Varlock item decorators reference
APP_ENV accepts dev, test, staging, and prod. The members are part of this schema, and Varlock checks for an exact match. The first two runs use test. The third uses testing, a plausible typo that resolves to a string but fails membership validation. The fixture omits the @currentEnv root decorator so changing APP_ENV exercises the enum without selecting a different dotenv file for each case. That keeps the file layout the same as you change the inputs. Varlock data types reference Varlock environments guide
The token is long and distinctive so a match in the capture points to the fixture value. Varlock’s secrets guide explains that short sensitive strings can collide with ordinary text. For example, choosing a synthetic value that also appears in a startup message would make it harder to tell which occurrence you were examining. Keep the environment name visible so the valid capture tells you that the process received test as well as a value for the token. Varlock secrets management guide
If you use an environment readiness procedure for agent workspaces, use it to make Node and Varlock available before you start. An installation that works only through a shell alias or function will need an executable on PATH for the recorder below. Keeping the fixture outside the repository’s ordinary launch path makes problems easier to trace before you involve a test runner, package scripts, or a larger configuration graph. AI agent environment readiness
Give the Node process a marker and two log lines
- Save the following block as
startup.cjsnext to.env.schema. The.cjsextension selects CommonJS even if a surrounding project treats.jsfiles as ES modules. - Keep
FIXTURE_STARTEDon the first application line andFIXTURE_ERROR_SAMPLEon the second. PreserveSTARTUP_TOKEN=in both lines so the checker can verify that the token field survived and its value changed in the capture.
const { APP_ENV, STARTUP_TOKEN } = process.env;
console.log(`FIXTURE_STARTED APP_ENV=${APP_ENV} STARTUP_TOKEN=${STARTUP_TOKEN}`);
console.error(`FIXTURE_ERROR_SAMPLE STARTUP_TOKEN=${STARTUP_TOKEN}`);
The first statement reads the environment supplied to the child. console.log writes the startup marker, the mode, and the synthetic token to stdout. console.error writes the second sample to stderr. The program returns normally after those two writes, so the expected valid exit code is zero. The stderr line is deliberate. A successful process can write to stderr, and using a distinct marker there tells you whether you actually exercised that stream. A real application’s first reliable entry-point message can serve the same diagnostic purpose without printing a credential.
The launch uses varlock run --inject vars to inject the individual resolved variables while omitting the __VARLOCK_ENV serialized configuration graph. This program reads process.env directly and needs no framework integration. The -- marks the end of Varlock’s options, and node startup.cjs is the child command. The shell receives that command as written. The token is read inside the launched Node process, after Varlock resolves and injects its value. Varlock CLI load and run reference
Capture the valid launch on both streams
- With the valid
.env.localin place, save the following block ascapture.sh. Runsh capture.sh validfrom inside the fixture directory. The prefixvalidties the status and the two streams to the same case. - Confirm that
valid.status,valid.stdout, andvalid.stderrexist. Compare the number and the child lines with the example below. Keep the streams in separate files through verification so that a missing line or a misplaced marker points to the affected stream.
#!/bin/sh
case "$1" in
valid|missing|invalid) name=$1 ;;
*) printf '%s\n' 'Usage: sh capture.sh valid|missing|invalid' >&2; exit 2 ;;
esac
if env -i PATH="$PATH" HOME="$HOME" varlock run --path ./ \
--inject vars --redact-stdout -- node startup.cjs \
> "$name.stdout" 2> "$name.stderr"
then
status=0
else
status=$?
fi
printf '%s\n' "$status" > "$name.status"
The recorder checks the requested prefix before starting Varlock. Use the same prefix to retry a case after restoring that case’s inputs. Run the attempts in sequence and let each finish before opening its files. The files are opened for replacement, so an interrupted attempt or a file creation error means you should rerun the whole case before interpreting its captures. The if block saves the wrapped command’s status immediately. Read that value from .status. The shell script’s own final operation writes the status file, so sh capture.sh valid can complete successfully even when the wrapped command returned nonzero.
env -i starts Varlock with a small inherited environment. PATH makes the installed executables available, and HOME preserves the usual home directory for the tools. The command drops exported overrides such as APP_ENV, STARTUP_TOKEN, NODE_OPTIONS, and _VARLOCK_USE_FROZEN_ENV. Varlock gives process overrides higher precedence than dotenv values. The clean launch lets an edit to .env.local test the intended input instead of repeating whatever happened to be exported in your terminal. The explicit --path ./ points Varlock at the directory holding the two fixture files. Varlock environments guide Varlock CLI load and run reference
The CLI calls the flag --redact-stdout and documents it as an override for stdout and stderr. In the recorder, > redirects file descriptor 1 and 2> redirects file descriptor 2. Each stream goes to a file. Forced redaction makes it an error to leave output attached to an interactive terminal. The flag takes precedence over the redaction environment override, which matters if you later compare with an ordinary launch that inherits more settings. Varlock CLI load and run reference
Compare the child lines separately from any CLI diagnostics. The sample below shows the documented retained prefix fi and the mask glyph ▒. The checker requires the prefix and one or more glyphs without fixing their count. The names in the left column identify files and are outside the file contents. Varlock secrets management guide
valid.stdout FIXTURE_STARTED APP_ENV=test STARTUP_TOKEN=fi▒▒▒▒▒
valid.stderr FIXTURE_ERROR_SAMPLE STARTUP_TOKEN=fi▒▒▒▒▒
valid.status 0
A real test may send machine-readable output to stdout. Before adapting the command, examine the payload the next step actually consumes. A replacement can change a JSON field while leaving the document valid JSON. If a downstream parser needs the original field, move human diagnostics to a suitable log stream and treat the data channel according to its own contract. This fixture uses both streams for sample log lines, so replacing the token is the desired effect. Varlock secrets management guide
Remove the token and try an invalid name
- Replace
.env.localin full with the missing-token block. Runsh capture.sh missing. Leave theSTARTUP_TOKENdeclaration in the schema and omit the entire assignment from.env.local. - Replace
.env.localin full with the invalid-name block. Runsh capture.sh invalid. Restoring the token holds that input constant whileAPP_ENV=testingintroduces the enum failure. - Restore the original valid
.env.localafter both captures finish. The three prefixes preserve the individual outcomes and leave the valid input ready for troubleshooting.
Missing-token .env.local:
APP_ENV=test
Invalid-name .env.local:
APP_ENV=testing
STARTUP_TOKEN=fixture-startup-token-9472
| Input | Action | Expected result |
|---|---|---|
task:varlock-valid-01 with APP_ENV=test and STARTUP_TOKEN=fixture-startup-token-9472 |
Save the valid .env.local and run sh capture.sh valid. |
Illustrative. valid.status is 0. The two child messages appear in their respective streams, each with a masked value after STARTUP_TOKEN=. |
task:varlock-missing-01 with APP_ENV=test and no STARTUP_TOKEN assignment in .env.local |
Save the missing-token block and run sh capture.sh missing. |
Illustrative. missing.status is nonzero, the diagnostic identifies STARTUP_TOKEN, and neither stream contains FIXTURE_STARTED. |
task:varlock-invalid-01 with APP_ENV=testing and STARTUP_TOKEN=fixture-startup-token-9472 |
Save the invalid-name block and run sh capture.sh invalid. |
Illustrative. invalid.status is nonzero, the diagnostic identifies APP_ENV, and neither stream contains FIXTURE_STARTED. |
Varlock returns the child’s exit status when the child runs. When configuration validation fails before launch, Varlock returns nonzero without starting the child. A setup failure can also produce a nonzero status and an absent marker. For example, if the node executable cannot start, the capture still lacks FIXTURE_STARTED. Use the diagnostic to confirm that Varlock actually rejected the intended item. The reference gives a nonzero requirement for validation failure, so the two negative checks accept any positive shell exit status. Varlock CLI load and run reference
If the missing case reports an APP_ENV validation error, check that you replaced the file in full and put APP_ENV=test back. If the invalid case reports a STARTUP_TOKEN requirement error, check that you restored the token on its own line. If both negative cases fail with the same schema parsing error, return to the valid case and repair the shared schema first. Changing one input at a time only helps once the baseline shows that the unchanged files parse and the child can launch.
Check the captures before making a handoff
- Save the checker below as
check.cjsand runnode check.cjsfrom the fixture directory. It reads the three files for each case, looks for the expected status and child messages, and searches both streams for the literal synthetic token. - Expect
valid passed,missing passed, andinvalid passedin order. A failed assertion sets a nonzero process status. A missing file also fails the invocation. If a run stopped early, capture that case again before using the verdicts. - Read the relevant diagnostic locally before sharing it. For a validation failure, select the case label, mode, status, the diagnostic that identifies the rejected field, and whether the startup marker appeared. Keep the local configuration file out of the handoff.
const { readFileSync } = require('node:fs');
const token = 'fixture-startup-token-9472';
const marker = 'FIXTURE_STARTED';
for (const [name, invalidKey] of [
['valid', null],
['missing', 'STARTUP_TOKEN'],
['invalid', 'APP_ENV'],
]) {
const stdout = readFileSync(`${name}.stdout`, 'utf8');
const stderr = readFileSync(`${name}.stderr`, 'utf8');
const status = readFileSync(`${name}.status`, 'utf8').trim();
const issues = [];
if (stdout.includes(token) || stderr.includes(token)) {
issues.push('literal fixture token found in captured output');
}
if (invalidKey === null) {
if (status !== '0') issues.push('expected zero exit');
if (!/^FIXTURE_STARTED APP_ENV=test STARTUP_TOKEN=fi▒+\r?$/m.test(stdout)) {
issues.push('expected startup line and mask on stdout');
}
if (!/^FIXTURE_ERROR_SAMPLE STARTUP_TOKEN=fi▒+\r?$/m.test(stderr)) {
issues.push('expected error sample and mask on stderr');
}
} else {
if (!/^[1-9][0-9]*$/.test(status)) issues.push('expected nonzero exit');
if (stdout.includes(marker) || stderr.includes(marker)) {
issues.push('child startup marker appeared');
}
if (!stdout.includes(invalidKey) && !stderr.includes(invalidKey)) {
issues.push(`expected a diagnostic naming ${invalidKey}`);
}
}
if (issues.length) {
process.exitCode = 1;
console.error(`${name} failed — ${issues.join(', ')}`);
} else {
console.log(`${name} passed`);
}
}
The valid check requires positive evidence from each stream. An empty valid.stderr can pass a literal token search, but the checker will still report that the error sample and mask are missing. Likewise, a stdout line with APP_ENV=dev fails even though dev belongs to the enum. The fixture checks that this launch received the specific test input. In a rejected case, a matching key name can also occur in unrelated diagnostics, so read the line that explains why the value failed validation. The checker prints verdicts and issue descriptions without echoing the captured lines.
Here is a filled handoff for the invalid-name case. Use it as an illustrative example and substitute your observed status and diagnostic after running the fixture. The requested change targets one value, and the rerun specifies which visible result should change. Write a finish line the transcript can prove
Case task:varlock-invalid-01 (illustrative)
Input APP_ENV=testing
Varlock exit nonzero
Diagnostic identifies APP_ENV
FIXTURE_STARTED absent from both captures
Change APP_ENV to test, retain the synthetic token, then run sh capture.sh valid and node check.cjs.
Expected recovery: valid passed, with APP_ENV=test on valid.stdout and masked token fields in both streams.
Diagnose a result that contradicts the fixture
| Observation | First check | Next decision |
|---|---|---|
| The missing case starts | Confirm missing.status came from a fresh run with the two-item schema and the single-line missing-token fixture. |
Inspect STARTUP_TOKEN resolution and remove the unexpected source from the fixture inputs. |
| The valid run fails before the marker | Read both valid captures locally for command, schema, or TTY errors. |
Fix the first reported prerequisite and rerun valid with the valid local file. |
| The marker shows a different allowed mode | Check where the launch resolved APP_ENV and which directory it loaded. |
Restore the scratch case to test and inspect project overrides before adapting the command. |
| One stream contains plaintext in an adapted run | Inspect the actual destinations of stdout and stderr. | Redirect both at the wrapper and repeat the check with the synthetic token. |
An inherited value is especially misleading in the missing case. If the shell exports STARTUP_TOKEN, deleting the dotenv assignment can leave a higher-precedence process value available in an ordinary launch. To see whether that explains a difference between the clean recorder and your usual command, compare resolution on the same restored valid input. Run these from the fixture directory. Varlock environments guide Prove headless runs ignore state you didn’t declare
env -i PATH="$PATH" HOME="$HOME" varlock explain --path ./ STARTUP_TOKEN
varlock explain --path ./ STARTUP_TOKEN
env -i PATH="$PATH" HOME="$HOME" varlock explain --path ./ APP_ENV
varlock explain --path ./ APP_ENV
explain reports definitions, overrides, and the resolved value, with sensitive values masked. Compare which definition supplies the value. In the clean baseline, the value-bearing definition should come from .env.local. If the ordinary shell shows a process override, inspect the script or editor task responsible for setting it. If you see an unexpected dotenv source in a real project, check the directory and whether an @currentEnv decorator selects a matching file. Matching environment-specific files can outrank .env.local, and process overrides take highest precedence. Varlock CLI load and run reference Varlock environments guide
TTY behavior can explain why a capture differs from a line you saw live. In automatic mode, Varlock checks each stream’s destination. Piping stdout alone can leave stderr attached to the terminal, where it passes through without stream redaction. With forced redaction, an interactive output stream causes an error. If an editor launches the command, check the redirections in the task definition and any intervening wrapper. Compare the actual files from the recorder when deciding whether your capture path works. Terminal scrollback can contain output from a different invocation. Varlock CLI load and run reference
If a real test emits a split-value warning, review where that output was saved. Varlock documents that delayed split writes can expose an initial part and recommends rotating the affected value if the output was saved. Inspect direct application log files separately and check whether the application logs transformed values. The fixture searches for one known string in two captures. Its assertions give you a result for that token, those two expected lines, and the selected stream destinations. Varlock secrets management guide
Restore the launch and keep the evidence you need
- If you only ran the scratch fixture, leave its directory after retaining any reviewed excerpts you need. Remove the
varlock-log-fixturedirectory you created for the exercise. It holds the schema, local values, two Node files, shell recorder, and nine capture files. The exercise requires no edit to the repository’s original test command. - If you added the wrapper to a package script in the scratch copy, restore the original script command and its original source of environment values together. Remove the
varlock runprefix and flags from that command and run the original test using its ordinary inputs. The launch then relies on its original input handling, and Varlock’s validation, injection, and stream filtering no longer apply to it. Varlock CLI load and run reference - If you keep the regression check, retain the synthetic input and the expected signals with the fixture. Remove diagnostic statements that interpolate real credentials from the application. When keeping an excerpt for an agent handoff, record the invoking command and input case with it so the next reader can reproduce the same observation. You Can’t Replay What You Can’t See
The trusted child receives the plaintext STARTUP_TOKEN in process.env. Omitting the serialized graph with --inject vars still leaves the individual token available to Node. That access is what lets the program exercise the filters on both of its output streams. For a coding-agent handoff, use the reviewed excerpt and synthetic reproduction. Keep real credentials scoped to the trusted processes that need them. Varlock CLI load and run reference Varlock secrets management guide Securing AI agents with least privilege

