{"id":17963289,"url":"https://github.com/sparanoid/env.sh","last_synced_at":"2025-07-23T17:08:27.854Z","repository":{"id":37210818,"uuid":"482219741","full_name":"sparanoid/env.sh","owner":"sparanoid","description":"Dead-simple .env file reader and generator","archived":false,"fork":false,"pushed_at":"2022-09-05T15:50:46.000Z","size":26,"stargazers_count":26,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-13T09:45:17.976Z","etag":null,"topics":["bash","create-next-app","docker","nextjs","reactjs"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sparanoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-16T10:09:44.000Z","updated_at":"2025-01-29T20:51:50.000Z","dependencies_parsed_at":"2023-01-17T14:01:34.732Z","dependency_job_id":null,"html_url":"https://github.com/sparanoid/env.sh","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sparanoid/env.sh","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparanoid%2Fenv.sh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparanoid%2Fenv.sh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparanoid%2Fenv.sh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparanoid%2Fenv.sh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sparanoid","download_url":"https://codeload.github.com/sparanoid/env.sh/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sparanoid%2Fenv.sh/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266717708,"owners_count":23973384,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","create-next-app","docker","nextjs","reactjs"],"created_at":"2024-10-29T11:25:08.313Z","updated_at":"2025-07-23T17:08:27.841Z","avatar_url":"https://github.com/sparanoid.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# env.sh\n\nDead-simple `.env` file reader and generator\n\n## Features\n\n- Designed to be used for Next.js app inside a Docker container (General React app should also work)\n- Read env file and generate `__env.js` file for runtime client use\n- Merge current environment variables passing to it (Useful for Docker images)\n- No dependencies (More like a lite version of [react-env](https://github.com/andrewmclagan/react-env)). But does not require you to build your project inside the container. The benefit of this method can help you avoid multistage Dockerfile. You can build your Next.js project in GitHub Actions. And only `COPY` built files required for production. With this method your image can be much smaller.\n\n## Usage\n\nSimply copy `env.sh` to the root of your project. And follow the steps.\n\nGeneral usage:\n\n```shell\n$ ./env.sh\n```\n\nReplacing varaible:\n\n```shell\n$ NEXT_PUBLIC_API_BASE=xxx ./env.sh\n```\n\nEnviroment variable not in whitelist will be discarded:\n\n```shell\n$ BAD_ENV=zzz ./env.sh\n```\n\nChange script options:\n\n```shell\n$ ENVSH_ENV=\"./.env.staging\" ENVSH_OUTPUT=\"./public/config.js\" ./env.sh\n```\n\nUse it with your existing project. Modify your scripts in `package.json`:\n\n```json\n\"scripts\": {\n  \"dev\": \"bash env.sh next dev\",\n  \"build\": \"CI=true bash env.sh next build\",\n}\n```\n\nCreate a `utils.js` to use it in runtime client:\n\n```js\n// Simplified from:\n// https://github.com/andrewmclagan/react-env/blob/master/packages/node/src/index.js\nexport function env(key = '') {\n  if (!key.length) {\n    throw new Error('No env key provided');\n  }\n\n  if (isBrowser() \u0026\u0026 window.__env) {\n    return window.__env[key] === \"''\" ? '' : window.__env[key];\n  }\n\n  return process.env[key] === \"''\" ? '' : process.env[key];\n}\n```\n\nIn `_document.js`:\n\n```js\n\u003cHead\u003e\n  \u003cscript src=\"/__env.js\" /\u003e\n\u003c/Head\u003e\n```\n\nIn `MyComponent.js`:\n\n```js\nimport { env } from 'utils';\nconst API_BASE = env('CUSTOM_API_BASE');\n```\n\nNow you can run `yarn dev` and see `API_BASE` dynamically injected to your app for client-side rendering.\n\nUse it inside `Dockerfile`:\n\nInstall `bash` for your image first (Here operator used in this script does not work with `sh` at the moment. This can be improved in the furture):\n\n```dockerfile\nRUN apk add --no-cache bash\n\n# ...other build steps\n\nRUN chmod +x ./env.sh\nENTRYPOINT [\"./env.sh\"]\n\n# ...custom next.js server if required\nCMD [\"node\", \"server.js\"]\n```\n\n## Options\n\n### `ENVSH_ENV`\n\nSpecify env file to read.\n\nDefault: `./.env`\n\n### `ENVSH_PREFIX`\n\nOnly environment variables with this prefix will be matched.\n\nDefault: `NEXT_PUBLIC_`\n\n### `ENVSH_PREFIX_STRIP`\n\nIf set to `true`, `ENVSH_PREFIX` will be stripped from the variable name:\n\n```js\nwindow.__env = {\nAPI: \"https://openbayes.com/\",\nDEBUG: \"true\",\n}\n```\n\nWhen set to `false`:\n\n```js\nwindow.__env = {\nNEXT_PUBLIC_API: \"https://openbayes.com/\",\nNEXT_PUBLIC_DEBUG: \"true\",\n}\n```\n\nDefault: `true`\n\n### `ENVSH_PREPEND`\n\nString to be prepended to the output config name. If you change this, you should also change the way to access it in `utils.js`.\n\nDefault: `window.__env = {`\n\n### `ENVSH_APPEND`\n\nString to be appended to the output config name.\n\nDefault: `}`\n\n### `ENVSH_OUTPUT`\n\nThe filename of the output config file.\n\nDefault: `./public/__env.js`\n\n### `ENVSH_VERBOSE`\n\nDebug level output.\n\nDefault: `false`\n\n## Security Alert\n\nThis script is used to read environment variables from a file and inject matched variables for client-side use. So It's not safe to prepend your private API keys or tokens with `ENVSH_PREFIX`. If you do that these variables will be available in `ENVSH_OUTPUT` file and exposed to the clients.\n\n## License\n\nApache-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsparanoid%2Fenv.sh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsparanoid%2Fenv.sh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsparanoid%2Fenv.sh/lists"}