Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dependency cycle detected import/no-cycle

I am trying to set up API endpoints in ES6. In my main server file, I tried to import the router module but I get the error "dependency cycle detected import/no-cycle". Please find my code below for clearance and assistance.

import express from 'express';

import bodyParser from 'body-parser';

import router from './routes/routes';

const app = express();
const PORT = process.env.PORT || 8080;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(routes);

app.use('/api/v1', router);

const run = () => console.log('way to go server!');

app.listen(PORT, run);
export default app;
like image 611
Darotudeen Avatar asked Jun 29 '18 04:06

Darotudeen


People also ask

How do I fix circular dependency in react?

This circular relationship caused a problem and webpack could not resolve the dependencies correctly. Our solution was to upgrade our modules to use ES6 import/exports. This enabled us to reuse the react components and avoid circular dependencies while moving us closer to ES standards.

What is a dependency cycle?

A dependency cycle is a relationship between two or more domains that lead to a situation where a slave domain depends on itself, or a master domain depends on one of its slave domains. The Logical Domains Manager determines whether a dependency cycle exists before adding a dependency.

How do you deal with circular dependencies?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

What is circular dependency detected angular?

A cyclic dependency exists when a dependency of a service directly or indirectly depends on the service itself. For example, if UserService depends on EmployeeService , which also depends on UserService . Angular will have to instantiate EmployeeService to create UserService , which depends on UserService , itself.


1 Answers

This could be a direct reference (A -> B -> A) issue, which even you might be doing.

// file a.ts
import { b } from 'b';
...
export a;

// file b.ts
import { a } from 'a';
...
export b;

Read HERE more about "Eliminate Circular Dependencies from Your JavaScript Project":

Once I had the issue in vue.js project and the code that had issue was something like this:

<script>
  import router from '@/router';
  import { requestSignOut } from '../../api/api';

  export default {
    name: 'sign-out',
    mounted() {
      requestSignOut().then((data) => {
        if (data.status === 'ok') {
          router.push({ name: 'sign-in' });
        }
      });
    },
  };
</script>

Then I fixed it this way:

<script>
import { requestSignOut } from '@/api/api';

export default {
  name: 'sign-out',
  mounted() {
    requestSignOut().then((data) => {
      if (data.status === 'ok') {
        this.$router.push({ name: 'sign-in' });
      }
    });
  },
};
</script>
like image 179
Syed Avatar answered Sep 19 '22 17:09

Syed