警告:在循环依赖中访问不存在的 属性 'sequelize' 模块导出
Warning: Accessing non-existent property 'sequelize' of module exports inside circular dependency
问题
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
可以运行正常访问网站。
但是我该如何解决呢?
ps。每次你 运行 号码都是独一无二的。 --> (节点:11528)
其他问题
当我使用 --> "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: []
}
当我使用 --> "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" : 网站无法正常运行。但是我该如何解决呢?
节点:v16.13.1
续集:v6.12.4
Nodemon:v2.0.15
数据库:mySQL
代码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)
})
这是第一个问题。如果有任何错误,在此表示歉意。
谢谢。
有几个问题
- 当您 运行
--trace-warnings
时,您指向的是一个不存在的文件。您需要 运行 它在同一个文件中,例如node --trace-warnings server.js
.
- nodemon 调用遇到了类似的问题,您可以尝试
nodemon -e js,ejs server.js
看看输出是什么
- 这是导致您出现问题的行。该错误表明
sequelize
未从此文件中导出。也许你的意思是 const sequelize = require('./models')
?您必须包含 ./models
中的代码才能确定
const { sequelize } = require("./models")
问题
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
可以运行正常访问网站。 但是我该如何解决呢?
ps。每次你 运行 号码都是独一无二的。 --> (节点:11528)
其他问题
当我使用 --> "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: []
}
当我使用 --> "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" : 网站无法正常运行。但是我该如何解决呢?
节点:v16.13.1 续集:v6.12.4 Nodemon:v2.0.15 数据库:mySQL
代码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)
})
这是第一个问题。如果有任何错误,在此表示歉意。
谢谢。
有几个问题
- 当您 运行
--trace-warnings
时,您指向的是一个不存在的文件。您需要 运行 它在同一个文件中,例如node --trace-warnings server.js
. - nodemon 调用遇到了类似的问题,您可以尝试
nodemon -e js,ejs server.js
看看输出是什么 - 这是导致您出现问题的行。该错误表明
sequelize
未从此文件中导出。也许你的意思是const sequelize = require('./models')
?您必须包含./models
中的代码才能确定
const { sequelize } = require("./models")