EJS provides a comprehensive command-line interface for rendering templates directly from the terminal, supporting various input methods, output options, and template configuration.
The EJS CLI is installed as a global command when the package is installed.
# Basic usage
ejs [options ...] template-file [data variables ...]
# Installation provides the 'ejs' command
npm install -g ejsUsage Examples:
# Render template with data file
ejs ./template.ejs -f ./data.json
# Render with inline data variables
ejs ./template.ejs name=John age=30
# Render with custom delimiters and output to file
ejs -m $ ./template.ejs -f ./data.json -o ./output.html
# Pipe data via stdin
echo '{"name": "Alice"}' | ejs ./template.ejs
# Remove whitespace and use custom delimiters
ejs -w -p [ -c ] ./template.ejs -o ./output.htmlThe CLI supports extensive configuration options for template rendering:
# Output Options
-o, --output-file FILE Write rendered output to FILE rather than stdout
-f, --data-file FILE Use JSON-formatted input from FILE as data
-i, --data-input STRING Use JSON-formatted URI-encoded STRING as data
# Delimiter Options
-m, --delimiter CHARACTER Use CHARACTER with angle brackets (defaults to %)
-p, --open-delimiter CHARACTER Use CHARACTER instead of left angle bracket
-c, --close-delimiter CHARACTER Use CHARACTER instead of right angle bracket
# Template Options
-s, --strict Generate function in strict mode
-n, --no-with Use 'locals' object instead of 'with' statement
-l, --locals-name NAME Name for object storing local variables
-w, --rm-whitespace Remove all safe-to-remove whitespace
-d, --debug Output generated function body
# Help Options
-h, --help Display help message
-V, -v, --version Display EJS versionThe CLI supports multiple ways to provide data to templates, in order of precedence:
# 1. Stdin (highest precedence)
echo '{"title": "Hello"}' | ejs template.ejs
# 2. Inline data input via -i flag
ejs template.ejs -i '{"title":"Hello"}'
# 3. Data file via -f flag
ejs template.ejs -f data.json
# 4. Individual variables (lowest precedence, but override others)
ejs template.ejs title="Hello" name="World"Data Input Rules:
Customize template syntax using delimiter options:
# Use $ instead of % (produces <$ $> tags)
ejs -m $ template.ejs -f data.json
# Use square brackets instead of angle brackets
ejs -p [ -c ] template.ejs -f data.json
# Produces [% %] syntax
# Combine both for custom syntax
ejs -m @ -p { -c } template.ejs -f data.json
# Produces {@ @} syntaxControl template compilation and output:
# Strict mode - disables 'with' statement
ejs -s template.ejs -f data.json
# No-with mode - uses locals object instead of 'with'
ejs -n template.ejs -f data.json
# Custom locals name with no-with
ejs -n -l customLocals template.ejs -f data.json
# Remove whitespace for minified output
ejs -w template.ejs -f data.json
# Debug mode - shows generated JavaScript
ejs -d template.ejs -f data.jsonDirect rendered output to files or stdout:
# Output to stdout (default)
ejs template.ejs -f data.json
# Output to specific file
ejs template.ejs -f data.json -o output.html
# Chain with other commands
ejs template.ejs -f data.json | gzip > output.html.gzThe CLI provides clear error messages for common issues:
# Missing template file
$ ejs
Error: Please provide a template path. (Run ejs -h for help)
# Multiple data input methods
$ echo '{}' | ejs template.ejs -f data.json
Error: Please do not pass data multiple ways. Pick one of stdin, -f, or -i.
# Invalid JSON data
$ ejs template.ejs -f invalid.json
SyntaxError: Unexpected token in JSON
# Template file not found
$ ejs missing.ejs
Error: ENOENT: no such file or directory, open 'missing.ejs'Get version information and usage help:
# Display version
ejs --version
# or
ejs -V
# or
ejs -v
# Display help with all options
ejs --help
# or
ejs -hComplex CLI usage patterns for various scenarios:
# E-commerce template with product data
ejs -w ./templates/product.ejs -f ./data/products.json -o ./dist/products.html
# Email template with custom delimiters to avoid conflicts
ejs -m @ ./email-template.ejs name="John" email="john@example.com"
# Debug template compilation
ejs -d ./complex-template.ejs -f ./test-data.json > debug-output.js
# Batch processing with variables
for user in alice bob charlie; do
ejs ./user-template.ejs name="$user" -o "./output/${user}.html"
done
# Pipeline processing with JSON data
curl -s https://api.example.com/data | ejs ./api-template.ejs -o result.html
# Custom locals name for conflict avoidance
ejs -n -l templateData ./template.ejs -f ./data.jsonCommon patterns for integrating EJS CLI with build systems:
# npm scripts in package.json
{
"scripts": {
"build:templates": "ejs -w ./src/template.ejs -f ./data/config.json -o ./dist/index.html",
"dev:watch": "chokidar './src/**/*.ejs' -c 'npm run build:templates'"
}
}
# Makefile integration
build:
ejs -w ./templates/layout.ejs -f ./config/prod.json -o ./public/index.html
# Shell script automation
#!/bin/bash
for template in ./templates/*.ejs; do
name=$(basename "$template" .ejs)
ejs -w "$template" -f "./data/${name}.json" -o "./output/${name}.html"
done