ReferenceError: Cannot access before initialization. Is this circular dependency?

Viewed 1737

In index.js, I have:

console.log("index.js loading...");
import express from "express";
export const app = express();

import { router as login } from "./login.js";
app.use("/login", login); 

In login.js I have:

console.log("login.js loading...");
import express from "express";
import { app } from "./index.js";
import passport from "passport";

app.use(passport.initialize());

When trying to run, I get a ReferenceError: Cannot access 'app' before initialization at the last statement in login.js. The line "login.js loading..." is printed, but not "index.js loading...".

While the two files are importing from one another, isn't app fully initialized in the third line in index.js? How is the order of loading determined? Would the error be there if index.js was loaded first?

0 Answers
Related