In this episode, we tackle a really important step for setting up any Nuxt app: moving your config values, like API keys and URLs, out of your code and into a .env
file. This change makes it easier to manage different settings across development, staging, and production, and it helps keep sensitive info safe.
We start by creating a .env
file in the root of the project and add things like REVERB_KEY
, REVERB_HOST
, and any other values you might need. Then, we jump over to the Nuxt config and set up the runtime config
, defining which values should be public and accessible in the client. I show you how to use process.env
to pull your variables into the runtime config.
Next, we update the plugin so it uses the useRuntimeConfig
composable. You'll see how to access your config inside the plugin, grab those public values, and swap out any hardcoded URLs or keys. We do a quick console log to see what the config looks like and make sure everything's working.
Finally, we go through how you can use the same approach in your components, for example when you need a base API URL. We add a new variable to the .env
file, use it in the runtime config, and then pull it into your fetch call. I mention that we'll refactor API requests later, but for now, this pattern is a solid starting point for managing all your environment-specific settings!