JSPM

  • Created
  • Published
  • Downloads 58599
  • Score
    100M100P100Q197767F
  • License ISC

envsub is envsubst for NodeJS

Package Exports

  • envsub

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (envsub) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

envsub is envsubst for NodeJS

NodeJS global CLI module providing environment variable substitution via Handlebars

npm install -g envsub
envsub templateFile outputFile

Example

  • Ensure environment variables exist
export MY_NAME=daniel
  • Create an environment variable template

template.html

Hello my name is {{ MY_NAME }}

  • Execute envsub
envsub template.html output.html
  • And the result

output.html

Hello my name is daniel

Docker Integration (optional)

envsubst is recognised by NGINX as a templating solution.

This module seeks to make envsubst freely available to the NodeJS community for Docker templating.

In both examples below the file ./files/public/index.html is a template file.

Build time templating / env substitution

Sample build time Dockerfile

docker build --build-arg MY_NAME=daniel -t danday74/envsub-build-example .
docker run --name envbuild -d -p "8080:8080" danday74/envsub-build-example

Run time templating / env substitution

Sample run time Dockerfile

docker build -t danday74/envsub-run-example .
docker run --name envrun1 -d -e MY_NAME=daniel -p "8081:8080" danday74/envsub-run-example
docker run --name envrun2 -d -e MY_NAME=jimbob -p "8082:8080" danday74/envsub-run-example

View Source

The codebase is intentionally simple and does exactly what you would expect.

View source HERE


Tips

Shorthand

Inside a docker container, it can be useful to overwrite a template file with its substituted equivalent.

To do this one would write

envsub file1 file1

The shorthand for which is

envsub file1

Advanced templating

All Handlebars templating is valid so you can do

{{#if MY_NAME}}
  My name is {{ MY_NAME }}
{{else}}
  I forgot my name
{{/if}}

For the docs refer to Handlebars

Hope this module proves useful out there. God bless :)