{"id":19112378,"url":"https://github.com/actionhero/example-with-custom-env","last_synced_at":"2026-01-21T08:33:11.136Z","repository":{"id":38947309,"uuid":"276415060","full_name":"actionhero/example-with-custom-env","owner":"actionhero","description":"An example to show how to modify the environment at boot, and integrate with the Actionhero config system","archived":false,"fork":false,"pushed_at":"2024-11-01T06:55:36.000Z","size":1520,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T20:24:17.838Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/actionhero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-01T15:31:02.000Z","updated_at":"2024-08-01T15:39:01.000Z","dependencies_parsed_at":"2023-02-17T08:16:48.812Z","dependency_job_id":"c5ebfcb9-aee0-4390-b943-04455a674713","html_url":"https://github.com/actionhero/example-with-custom-env","commit_stats":{"total_commits":265,"total_committers":6,"mean_commits":"44.166666666666664","dds":"0.12075471698113205","last_synced_commit":"007607ca6ae2a94151c7e119f014fbb1a59396e1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actionhero%2Fexample-with-custom-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actionhero%2Fexample-with-custom-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actionhero%2Fexample-with-custom-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/actionhero%2Fexample-with-custom-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/actionhero","download_url":"https://codeload.github.com/actionhero/example-with-custom-env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249565250,"owners_count":21292427,"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","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":[],"created_at":"2024-11-09T04:32:54.722Z","updated_at":"2026-01-21T08:33:11.103Z","avatar_url":"https://github.com/actionhero.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# example-with-custom-env\n\n![Node.js CI](https://github.com/actionhero/example-with-custom-env/workflows/Node.js%20CI/badge.svg)\n\nThis repository contains an Actionhero example showcasing how to modify the environment at runtime and integrate with the config system. Specifically, we focus on needing to rely on an `async` method to determine what these changes to `process.env` should be. Perhaps you need to load the config from an API or read them from the file system.\n\n## config/hero.ts and initializers/hero.ts\n\nThese are the 2 \"normal\" actionhero components that our config is trying to set up. We have a new entry for `config.hero.favorite` which we then want to log in an initializer.\n\n```ts\n// config/hero.ts\nconst namespace = \"hero\";\n\ndeclare module \"actionhero\" {\n  export interface ActionheroConfigInterface {\n    [namespace]: ReturnType\u003ctypeof DEFAULT[typeof namespace]\u003e;\n  }\n}\n\nexport const DEFAULT = {\n  [namespace]: () =\u003e {\n    return {\n      favorite: process.env.FAVORITE_HERO,\n    };\n  },\n};\n```\n\n```ts\n// initializers/hero.ts\nimport { Initializer, config, log } from \"actionhero\";\n\nexport class MyInitializer extends Initializer {\n  constructor() {\n    super();\n    this.name = \"hero\";\n  }\n\n  async initialize() {\n    log(`Loaded favorite hero: ${config.hero.favorite}`, \"warning\");\n  }\n}\n```\n\n## modules/asyncConfig and .env\n\nThis is our example method that will modify `process.env` within an async function.\n\n```ts\n// modules/asyncConfig\nimport * as path from \"path\";\n\n// I modify process.env from an .env file, and I'm async\nexport default async function getConfig() {\n  await sleep(); // to really demonstrate an async function\n  const envFile = path.join(__dirname, \"..\", \"..\", \".env\");\n  require(\"dotenv\").config({ path: envFile });\n}\n\nfunction sleep(time = 1000) {\n  return new Promise((resolve) =\u003e {\n    setTimeout(resolve, time);\n  });\n}\n```\n\nWe are using the [`dotenv`](https://www.npmjs.com/package/dotenv) package to load the contents of `.env` into our environment.\n\n```\n# normally, you shouldn't check this file into git, but this is an example.\n\nFAVORITE_HERO=BATMAN\n```\n\n## server.ts\n\nFinally, we load and run `getConfig()` as part of the setup of our server.\n\n```ts\n#!/usr/bin/env node\n\nimport asyncConfig from \"./modules/asyncConfig\";\n\nasync function main() {\n  // modifying the environment needs to happen before requiring or importing Actionhero\n  await asyncConfig();\n\n  const { Process } = await import(\"actionhero\"); // \u003c-- note the use of async import\n\n  // create a new actionhero process\n  const app = new Process();\n\n  // handle unix signals and uncaught exceptions \u0026 rejections\n  app.registerProcessSignals((exitCode) =\u003e {\n    process.exit(exitCode);\n  });\n\n  // start the app!\n  // you can pass custom configuration to the process as needed\n  await app.start();\n}\n\nmain();\n```\n\n_visit www.actionherojs.com for more information_\n\n## To install:\n\n(assuming you have [node](http://nodejs.org/), [TypeScript](https://www.typescriptlang.org/), and NPM installed)\n\n`npm install`\n\n## To Run:\n\n`npm start`\n\n## To Test:\n\n`npm test`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factionhero%2Fexample-with-custom-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factionhero%2Fexample-with-custom-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factionhero%2Fexample-with-custom-env/lists"}