Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
How to define environment variables in svelte?

I'm new to svelte and I'd like to use some environment variables like base_url in different components. I know that I can define store, and put those values there,like:

const Store = writable([
  { 
    base_url: "http://127.0.0.1:8080",
  }
]);

and each time I import store to compoenent using

let base_url = "";
import Store from "../stores/Store.js";
Store.subscribe((data) => {
  base_url = data.base_url  
});

But that involves lots of boilerplate code, and I find store more suitable for dynamic data rather than static values. So I'm wondering what is the more idomatic/convenient way to do so?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The way I mostly go about this is to create a file named environment.js or sth like that and put all my environment variables in there. Then I just need to import them where I need them.

// environment.js:
export const base_url = '..';

// somewhere-else.js/somecomponent.svelte:
import { base_url } from './path/to/environment.js';

This will also make it easier to replace these variables depending on whether you are in development or production mode. You can then easily add replacement solutions (replacing the variable value with what's fitting) which depend on what setup you use (Rollup: rollup-plugin-replace, Vite: env files, etc).

about 4 years ago · Juan Pablo Isaza Denunciar

0

My structure is like this:

src/config/endpoint.js

//src/config/endpoint.js

let base_url = '';

switch(ENV){
  case ENV_PROD:
    base_url = 'http://www.example.com/api';
  break;
  case ENV_TEST:
    base_url = 'http://www.example.com/test_api';
  break;
  default:
    base_url = 'http://localhost:3301/api';
  break;
}

export base_url;

Now import endpoint.js in your components or file and use like this:

//App.svelte

import { base_url } from './../config/endpoint.js';

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda