Prompt template renderer that fails on missing variables (Node.js)
A zero-dependency Node.js script that fills {{variable}} placeholders in a prompt file from JSON, supports nested keys and lists, and exits non-zero on missing or unused variables so broken prompts never reach a model.
File in Engineering by AISkillHub Library
What it does
Simple string replacement is how most prompt templates are filled, and it fails silently: a renamed variable leaves `{{customer_name}}` in the text the model receives, and nobody notices until outputs get strange. This renderer is strict by default.
**What it does**
- Replaces `{{name}}` and dotted paths such as `{{product.name}}` with values from a JSON file or stdin. - Strings go in as-is. Arrays of strings become a `- item` list. Other objects and arrays are inserted as pretty-printed JSON. Numbers and booleans are converted to text. - Removes a `--- ... ---` front matter block from the output, so prompts can carry metadata. - `\{{` produces a literal `{{` for prompts that need to show template syntax. - **Fails** with exit code 1 when a placeholder has no value, or when the JSON has keys the template never uses (catches renamed variables from the other side). Use `--allow-unused` to relax the second rule. - `--check` validates without printing; with no vars file it lists the template's variables, which is handy for generating forms or docs.
**Example**
The listing includes `prompts/release-notes.md` and a matching `release-notes.vars.json`:
``` $ node scripts/render-prompt.mjs prompts/release-notes.md prompts/release-notes.vars.json You write release notes for Acme Invoicing, a product used by finance teams at small companies. ... <changes> - feat: export invoices as CSV from the list view - fix: PDF totals rounded wrong when tax rate had three decimals ... ```
Pipe the output into any CLI or API call, or use `--out rendered.md`.
**What it will not do**
No conditionals, loops or filters. If you need those, you need a full template engine; this tool is for plain fill-in prompts where strictness matters more than features.
Exit codes: 0 rendered or check passed, 1 missing or unused variables, 2 usage error (unreadable file, invalid JSON).
How to use it
1. Copy `scripts/render-prompt.mjs` into your repository. The example files `prompts/release-notes.md` and `prompts/release-notes.vars.json` are optional. 2. Requires Node.js 18 or newer. No `npm install` needed. 3. Use it: ``` node scripts/render-prompt.mjs prompts/release-notes.md prompts/release-notes.vars.json node scripts/render-prompt.mjs prompts/release-notes.md vars.json --out rendered.md node scripts/render-prompt.mjs prompts/release-notes.md --check # list variables node scripts/render-prompt.mjs prompts/release-notes.md vars.json --check # validate only echo '{"version":"2.4.0"}' | node scripts/render-prompt.mjs prompts/x.md - --allow-unused ``` 4. In the example template, `{{product.name}}`, `{{product.audience}}`, `{{previous_version}}`, `{{version}}` and `{{changes}}` are the variables to fill; `changes` takes a list of strings. 5. Add `--check` runs to CI for each template and its sample vars file to catch drift.
No environment variables or secrets.
Files
Save each file at the path shown, relative to your project root.