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

305
Views
How to avoid circular import (dependency) in JS?

I have a simple example where I have a circular dependency issue. What can be done to avoid this importing loop?

store.js

import { fetchApi } from "./api";

class Store {
  auth = "zzzzz";

  fetch = () => fetchApi();
}

const store = new Store();
export default store;

api.js

import client from "./client";

export const fetchApi = () => client("/test");

client.js

import store from "./store";

const client = (endpoint) => {
  const config = {
    method: "GET",
    headers: {
      Authorization: `Bearer ${store.auth}`
    }
  };

  return window.fetch(endpoint, config);
};

export default client;

With this code, I have the following situation:

  • store is importing api
  • api is importing client
  • client is importing store

store -> api -> client -> store

Happy to hear suggestions or see examples on how can I avoid this pattern. ๐Ÿ™๐Ÿป

about 4 years ago ยท Juan Pablo Isaza
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!