dynamic-env-cli
v1.0.12
Published
A simple Node.js CLI tool and runtime helper to inject and access environment variables dynamically in frontend JavaScript files, especially useful for static sites served via Nginx.
Maintainers
Keywords
Readme
dynamic-env-cli
A simple Node.js CLI tool and runtime helper to inject and access environment variables dynamically in frontend JavaScript files, especially useful for static sites served via Nginx.
🚀 Features
- Generates a
run.shscript that:- Serializes environment variables into a string.
- Replaces
___ENV-VARIABLES___placeholder in built.jsfiles. - Starts the Nginx server.
- Provides a runtime function to access environment variables using
getEnvVariable. - Supports both
development(from.envor system) andproduction(injected string) environments.
📦 Installation
Install globally:
npm install -g dynamic-env-cliOr use it locally:
npm install dynamic-env-cli --save🔧 Usage
1. Adding dynamic-env-cli to your build script
Add the CLI tool to your build script in package.json:
{
"scripts": {
"build": "your-build-command && dynamic-env-cli"
}
}2. Build your frontend project (e.g., React, Vue, etc.)
npm run buildEnsure your build output is in a dist/ or build/ directory.
3. Run the generated script
./dist/run.shor
./build/run.sh🧠 Accessing Environment Variables in Code
import { getEnvVariable } from 'dynamic-env-cli';
const apiHost = getEnvVariable('API_HOST');
console.log('API Host:', apiHost);- In development, it returns the value from
process.env. - In production, it parses the injected
___ENV-VARIABLES___string and retrieves the variable.
📝 Example Workflow
# Build your frontend app
npm run build
./build/run.sh 🐳 Dockerfile Example
You can use the package inside a Docker container to prepare and serve a frontend app.
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
# Copy build output from builder stage (adjust to your build output folder)
COPY --from=builder /app/build/ .
CMD ["sh", "-c", "sh run.sh --nginx"]✅ Build and Run the Docker Image
docker build -t your-app .
docker run -p 80:80 --env-file .env your-app💡 Ensure your
.envfile is available at build time or mount it at runtime using--env-file.
⚠️ Error Handling
If the environment variables are not injected in production, the library will log a error and fallback will fail gracefully:
[ERROR] Environment variables are not injected yet.
Please ensure the "run.sh" script has executed successfully.👤 Author
Artur Aleksanyan
