# env-serve

> Serve static files with https and dynamic config

Latest version **1.1.0** (published 2025-08-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install env-serve
pnpm add env-serve
yarn add env-serve
bun add env-serve
```

Provides the command `env-serve`.

## Health

**Score 40/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-08-26 |
| First published | 2019-07-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dawid Wojda |
| Maintainers | dawiidio |
| Keywords | server, https, ssl, env, dynamic, config |

## Links

- npm: https://www.npmjs.com/package/env-serve
- Repository: https://github.com/dawiidio/env-serve
- Homepage: https://github.com/dawiidio/env-serve#readme
- Issues: https://github.com/dawiidio/env-serve/issues
- npm.io page: https://npm.io/package/env-serve

## Dependencies (4)

- [pem](https://npm.io/package/pem.md) ^1.14.2
- [commander](https://npm.io/package/commander.md) ^2.20.0
- [args-parser](https://npm.io/package/args-parser.md) ^1.1.0
- [serve-handler](https://npm.io/package/serve-handler.md) ^6.1.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2025-08-26
- 1.0.3 — 2020-04-29
- 1.0.2 — 2019-08-05
- 1.0.1 — 2019-07-25
- 1.0.0 — 2019-07-25

## README

# env-serve

HTTP(S) server for static files with dynamic config from env vars 

### Examples

```bash
### install
npm i -g env-serve

# for help
env-serve --help

# run simple server
env-serve

# change port and entry file
env-serve -p 3003 -f foo_bar.html

# run https server with selfsigned certs
env-serve -s -p 3003 -f index.html

# run server and change coanfig basing on env vars
EXPORT MY_TEST_VAL="My test value"; env-serve -f index.html

# change file type from html to js, now config.js should contains window.appConfig variable
env-serve -f config.js

# change default global config var name, now you should rename appConfig to fooBar
env-serve -g fooBar

# override config values via CLI options (string, numbers, booleans, nested using dot path)
# e.g. set apiUrl
env-serve --option "apiUrl=http://localhost:5000"
# multiple overrides are supported
env-serve -o "apiUrl=http://localhost:5000" -o "feature.enabled=true" -o "retries=3" -o "nested.key=value"
```

### How it works?
Let's say you have an `index.html`:

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My test page</title>
    <script>
        window.appConfig = {
            "MY_TEST_VAL": "Hello world",
            "MY_TEST_VAL_2": 123
        };
    </script>
</head>
<body>
<h2>Application config:</h2>
<pre></pre>
<script>
    const c = document.querySelector('pre');
    c.innerText = JSON.stringify(window.appConfig, null, 4);
</script>
</body>
</html>
```

now you can control variables in `window.appConfig` on server start. For example:
```bash
EXPORT MY_TEST_VAL="My test value"; env-serve -f index.html
```

Server runs on port 3000 with changed configuration, instead of `Hello world` 
you should see `My test value`.

### Use case
It's helpful when you need to run several variants of your app and some parameters
needs to be passed on runtime.

It could be especially useful when mixed with docker, then you can do something like:

`docker run -p 3000:3000 -e MY_TEST_VAL="Foo bar" frontend:latest`

Thanks this you don't need to rebuild whole docker image with app to change your application behaviour 

Example Dockerfile for above example

```dockerfile
FROM node:12
ENV MY_TEST_VAL ''
COPY . /app/
WORKDIR /app
RUN npm install
RUN npm install -g env-serve
RUN npm run build
EXPOSE 3000
WORKDIR /app/build
CMD ["env-serve"]
``` 

### Known problems
- I can't quit from my docker when env-serve is set to entrypoint - add `--init` flag to your `docker run` it prevents env-serve process from stealing PID 1 and helps with proper signal handling  

Help output:
```text
Usage: env-serve [options]

Options:
  -V, --version                   output the version number
  -v, --version                   output the version number
  -g, --global [globalName]       global variable name eg. window.yourName (default: "appConfig")
  -c, --cert [pathToCert]         path to cert file
  -C, --ca [pathToCa]             path to ca file
  -S, --https [pathToCa]          is https mode (default: false)
  -k, --cert-key [pathToCertKey]  path to cert key file
  -p, --port [port]               port (default: 3000)
  -f, --config-file [configFile]  file where config exists (default: "index.html")
  -s, --self-signed [selfSigned]  generate self signed certificate for server
  -o, --option [key=value]        override config option, may be repeated (e.g. --option "apiUrl=http://localhost:5000")
  -h, --help                      output usage information
```

---
_Source: https://npm.io/package/env-serve · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
