Skip to content

A minimal dotenvx setup in a Node.js ecosystem for Railway ๐Ÿš…

License

Notifications You must be signed in to change notification settings

lfavreli/dotenvx-node-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

dotenvx in a Node.js environment

Welcome to the Railway dotenvx Node.js template!

This example provides a minimal setup for managing environment variables using the dotenvx library in a Node.js environment (e.g. http or express server) deployed on Railway's platform-as-a-service (PaaS).

Deploy on Railway

โš ๏ธ Disclaimer

This is a demonstration template.
The .env.keys file should not be commited in a real project.

(cf. Development and production use)

โœจ Overview

dotenvx is described as a better dotenvโ€“from the creator of dotenv :

  • run anywhere (cross-platform)
  • multi-environment
  • encrypted envs

๐Ÿš€ Use as it stands

  1. Click the 'Deploy on Railway' button ๐Ÿ‘†

  2. Deploy with the pre-configured environment variables values (production):

DOTENV_PRIVATE_KEY_PRODUCTION=706afc89eaa09ea9441d0f18f7c2fbbb6c77a201abf47aaa3a607e499a52c51d
  1. Click on the "public domain" link provided by Railway to observe the variables being loaded from the server.
`Hello ${process.env.NAME ?? 'world'} from ${process.env.ENVIRONMENT ?? 'space'}!`;

There you go! ๐Ÿ’ช

Please, note that there may be a delay of 2-3 minutes for the DNS to become aware of this new address. If this doesn't work after a few minutes, in the settings, delete the public address (Public Networking) and regenerate one.

  1. (Bonus) To use development environment variables, change the name and value of the private key:
DOTENV_PRIVATE_KEY=78ef7c8b904d674d8a2e468714e4b770a3c7c76b3c09cdfa1bc80bcd862f0036

๐Ÿ’ป Development and production use

  1. Installing dotenvx globally:
npm install @dotenvx/dotenvx -g

Install globally as a cli to unlock dotenv for ANY language, framework, or platform. ๐Ÿ’ฅ I am using (and recommending) this approach going forward. โ€“ motdotla

  1. Add (or uncomment) the .env.keys file in the .gitignore to prevent it from being tracked:
.env.keys

Then, at the root of the project, make sure you no longer track this file:

git rm -r --cached .

If there are, you can add and commit these changes.

  1. Encrypt your secrets and generate your own private keys file (.env.key):

    3.1. Remove existing .env.keys file:

    rm .env.keys

    3.2 Setting up your custom .env and .env.production files:

    NAME=YOUR_NAME
    ENVIRONMENT=YOUR_ENV
    YOUR_CUSTOM_KEY=YOUR_CUSTOM_VALUE
    

    Make sure you don't have a DOTENV_PUBLIC_KEY variable in your environment files to generate new keys.

    3.3. Encrypt created environment files:

    dotenvx encrypt -f .env
    dotenvx encrypt -f .env.production
  2. Finally, in Railway, update the variable with the value of the newly generated key: either DOTENV_PRIVATE_KEY or DOTENV_PRIVATE_KEY_PRODUCTION, depending on the variables you wish to load.


And that's it! You've just regenerated the encrypted file and decryption keys. Each time you add, delete, or modify environment variables in your .env files, check that the values are encrypted before deploying the file on your server. It's that simple!

โš’๏ธ Useful commands

  • Retrieve initial value(s) from and encrypted .env file:
dotenvx get {KEY} [-f,--env-file]                   # Return a single value
dotenvx get [-f,--env-file] [-pp,--pretty-print]    # Return all values
  • Set a new encrypted key/value:
dotenvx set {KEY} {VALUE} [-f,--env-file]
  • Decrypt an encrypted environment file:
dotenvx decrypt [-f,--env-file] [--stdout]
  • More information and details about the CLI:
dotenvx --help

๐Ÿ“„ Learn more

dotenvx:

dotenvx-node-template:

Contribution

Contributions are welcome! If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

This template is licensed under the MIT License. See the LICENSE file for details.