Use dotenv for environment vars? #122
Comments
Should we close this @arxpoetica? I think this is probably an app-level concern, rather than something Sapper needs to worry about |
Sure, that's fine. I'll close. |
Ok, I may have changed my mind about this. Managing environment variables is such a common chore that I've come round to thinking that maybe it makes sense to have an officially recognised 'correct' way to do it. My thinking is that if you have // generated by sapper build at 2018-08-01T22:14:14.378Z
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.SAPPER_DEST = __dirname;
process.env.PORT = process.env.PORT || 3000;
+require('sapper/env.js').setup();
console.log('Starting server on port ' + process.env.PORT);
require('./server.js');
Good idea? Bad idea? Things I've overlooked? |
As @johnmuhl says, an alternative is to do this: import 'dotenv/config'; Also, the |
Actually, The Twelve-Factor App which dotenv references argues against grouped "environments" (test, dev, production, staging, etc), which I can agree with, but this doesn't mean defaults aren't a good idea. Arguably, if it's a default, one could just encode it during startup and not even have it in a |
Single file FTW 👍 IMO default values are either belong in a config object (due to not being environmentally driven), or are environment-specific and live in that one-off file. |
I use a config.js which merges the defaults and require('dotenv').config()
module.exports = {
appId: process.env.APP_ID,
port: process.env.PORT || 3000,
url: process.env.APP_URL || 'http://localhost',
allowFrom: process.env.ALLOW_FROM,
mapApiKey: process.env.MAP_API_KEY,
defaultLocale: process.env.DEFAULT_LOCALE || 'en',
...
} |
@lukeed devil's advocate, what's the difference between two env files or just two config files for that matter? what you're describing is effectively the same thing, just different file extension names. |
Haha 😆 The difference is the environment that they're run in. And the difference in having the same environment split into multiple files (or not) is just a preference for having a single source of truth. It's a lot easier to guard one thing with your life than to guard multiple at the same time. And, at least for me, it'd be harder to remember/maintain which files are shareable, which ones are super sensitive, which need to be encrypted, etc etc. At the end of the day, it doesn't really matter. I think the |
@lukeed I don't think we're understanding each other. I'm not talking about different environments. I actually agree with the Twelve-Factor App that multiple environment files aren't good. I was strictly talking about defaults, i.e., what's the best place to put them. |
I think I'm pro-single file — it's easy enough to have defaults in a separate config file, as @elcobvg showed. And if that file is So I'll close this issue again. Thanks everyone! |
@arxpoetica Perhaps not, sorry! For default values (what I will also call static values), I will go about it one of two ways, depending on how many total environment variables I have. If I have a lot (couple dozen?), I typically feel like I need some more organization, so I will take the approach that @elcobvg mentioned. By constructing the config object, I get to embed the default config values within the code base in a place that's immediately relevant (IMO). For smaller projects, I will generally just include the default values directly within This might be a bad practice or something along those lines, but it's what I picked up after years of doing PHP work. :D |
My preference is to enable This provides the ability for teams to choose whether they want one file (as recommended by the 12-Factor) or multiple files (which tends to be easier to secure and also more flexible). |
So use |
Now |
Any instructions on how to set up sapper with I'm getting a node error during the compile.....
|
It seems the way sapper builds things probably means we need some specific guidance on how to do this, as the common examples are giving the |
I had the same problem and I tested a few things to make it work:
If anyone find how to fix it using |
There are two ways to use // Using import
import { config } from "dotenv";
config();
// Or using require
require('dotenv').config(); (remember this call needs to be done in If those do not work you can dotenv-cli (which you need install globally). From there you do not need to modify any code, you just need to prepend any commands with I have had success with both approaches. BUT it is important to note that anything on the frontend does not have access to @Sureiya @benwinding @MarcoBrunoBR |
Any way to pass .env variables to the client side? |
You can pass them as session data on to the client. Please come and talk to us on the discord channel for further discussions. |
You can use dotenv on server.js and pass this data to the client import dotenv from "dotenv";
const config = dotenv.config();
const { API_PATH } = process.env;
polka()
.use(
sapper.middleware({
session: (req, res) => ({
apiPath: API_PATH,
}),
})
) Then you can use it in a svelte file like this <script context="module">
export async function preload({ params }, session) {
const res = await this.fetch(`${session.apiPath}/word/${params.word}`);
.....
}
</script> |
I use a patch inspired by Antony Jones' "Svelte Most Wanted" talk. It reads config variables and values using dotenv and replaces all occurrences of For instance, you can declare
in your If you use string |
Hi there! Using this patch from @martinburger comment means that all the variables from .env will be compiled into the build, am I right? So, the app will not read variables from .env file/files on start. After changing some vars you'll need to rebuild the app. Maybe this is what some devs really need (I don't know), but if we want the app to read vars from .env files on startup, we can use dotenv or dotenv-flow package, but we will not be able to customize PORT, for example (since sapper defines it before we can initialize And we really can use dotenv-cli package here — just prepend npm scripts with
|
I can open an issue if it makes sense to, but I can't seem to get env vars to work in server routes. In
In
In
process.env.GENIUS_TOKEN works as expected in |
That's great. The only thing I find it hard to get working with is in context="module" cases. |
(Just putting this somewhere so I remember.)
This is one type of best practice. For example, I use this as a
config.js
file, which I can then load anywhere.It's the equivalent of doing
process.env.WHATEVS
, but it helps manage enviro vars both local and in production (such as aPORT
).The text was updated successfully, but these errors were encountered: