Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!