Skip to main content
Environment variables configure a game without changing its code: a server address, a port, a game mode.

Set them

Put them in a .env file at the root of the project. Their names start with NANOFORGE_, and the prefix is removed before the game sees them: NANOFORGE_GAME_MODE becomes GAME_MODE. In the .env file, NANOFORGE_CLIENT_X is meant for the client and NANOFORGE_SERVER_X for the server; both become X. See Environment variables in the CLI docs. Never put a secret in a variable the client receives: the client runs in the player’s browser. Put server-only values in the .env file with the NANOFORGE_SERVER_ prefix, and never export secrets in the shell or container environment that runs nf start or nf dev.

Read them

The run options carry the variables as raw strings: options.env in main, and ctx.env in a library’s __init. To check them and convert them to typed values, describe them with a class and pass it to registerEnv from @nanoforge-dev/env:
  • The declare module block types ctx.rules: see Writing your own library.
  • Mark every variable with @Expose(): the others are dropped.
  • @Default(value) gives the value of a missing variable. @TransformToBoolean() turns "true" and "false" into booleans. @IsIpOrURL() checks an address.
  • The package re-exports the class-validator decorators (@IsIn, @IsPort, @IsString, @IsOptional…) and Transform from class-transformer.
  • registerEnv throws NfEnvValidationException with every failed check, so a bad value stops the game at start-up instead of later.
The network library reads its own variables this way: see Network.