Search code examples
javascriptnode.jssequelize.jsnodemon

Warning: Accessing non-existent property 'sequelize' of module exports inside circular dependency


Problem

nodemon server.js

[nodemon] 2.0.15

[nodemon] to restart at any time, enter `rs`

[nodemon] watching path(s): *.*

[nodemon] watching extensions: js,mjs,json

[nodemon] starting `node server.js`

Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.     
(node:11528) Warning: Accessing non-existent property 'sequelize' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
Server running at port: 8000

It can run the website normally. But how can I fix it?

ps. every time you run The numbers are unique. --> (node:11528)

Other problems

When i use --> "node --trace-warnings sequelize"

PS C:\Users\company\Desktop\Project> node --trace-warnings sequelize
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\Users\company\Desktop\Project\sequelize'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

When i use --> "nodemon -e js,ejs index.js"

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,ejs
[nodemon] starting `node index.js index.js`
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\Users\company\Desktop\Project\index.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...

"node --trace-warnings sequelize" and "nodemon -e js,ejs index.js" : The website cannot run normally. But how can I fix it?

Node : v16.13.1 Sequelize : v6.12.4 Nodemon : v2.0.15 DB : mySQL

code server.js

require('dotenv').config()

const express = require('express')
const app = express()

const appRoutes = require('./routes')

const { sequelize } = require("./models")

const appSchedule = require("./schedules")

const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');


app.use(cookieParser("secret"));

//config session
app.use(session({
   secret: 'secret',
   resave: true,
   saveUninitialized: false,
   cookie: {
      maxAge: 1000 * 60 * 60 * 24 // 86400000 = 1 day
   }
}));

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

//Config passport middleware
app.use(passport.initialize());
app.use(passport.session());

sequelize.sync().then(() => {
    
    // static resources
    app.use(`/public/`, express.static('public'))

    // ejs view
    app.set('views', 'views')
    app.set('view engine', 'ejs')

    // app route
    app.use(express.json())
    app.use(express.urlencoded({
        extended: true
    }))
    app.use(`/`, appRoutes)

    // start schedule
    appSchedule.start()
    // listen connection
    app.listen(process.env.PORT, () => {
        console.log(`Server running at port: ${process.env.PORT}`)
    })    
}).catch((error) => {
    console.error(error)
})

It's the first question. If there is any mistake, I apologize at this point.

Thank you.


Solution

  • There are a couple issues

    1. When you run --trace-warnings, you are pointing to a non-existent file. You need to run it on the same file, e.g. node --trace-warnings server.js.
    2. The nodemon call suffers from a similar issue, you could try nodemon -e js,ejs server.js and see what the output is
    3. This is the line causing you issues. The error indicates that sequelize is not exported as from this file. Perhaps you meant const sequelize = require('./models')? You'd have to include the code from ./models to be sure
    const { sequelize } = require("./models")