Mustache CLI


This post goes over how to use mustache as a command-line interface (CLI).

Install

Install mustache globally:

npm i -g mustache

Or call the binary with npx:

npx mustache

Usage

See usage syntax:

mustache
Syntax: mustache <view> <template> [output]

Given view:

{
  "name": "world"
}

And template:

Hello, {{ name }}!

Running the command:

mustache view template

Outputs:

Hello, world!

To save the output as a file, run the command with an output argument:

mustache view template output

Without a file extension, mustache expects the view file to be JSON.

Add a .js extension for mustache to read the view file as JavaScript:

// view.js
module.exports = {
  name: 'world',
};
mustache view.js template

Demo

Replit example:



Please support this site and join our Discord!