Search code examples
global-variablesconfigsveltekit

How do I create global server config objects in sveltekit?


In my sveltekit app, I keep doing this in my +server.js files throughout my routes:

import config from '$lib/server/config.js';

where the exported config object contains database connection details, etc. (some of which in turn come from environment variables), and this information is adapted to the environment I am running in (dev, prod, etc.).

I have two questions about this. First, is there a canonical place to put such a config.js file other than where I am putting it?

Second, is there a way to load this config object globally so I don't need to load it repeatedly in every +server.js file that needs this information? I could put it all in environment variables (via .env, for example), but then I lose some of the conveniences of my config.js.


Solution

  • Based on the comments, it seems that $lib/server is a good home for configuration files. As far as global configuration variables, it appears that environment variables are the way to go, and can be set via dotconfig--making sure not to check your sensitive information into your repo where applicable (although I have gotten into the habit of creating a template called .env.ex to remind myself or others what variables it should contain).