gpt4 book ai didi

node.js - 错误: Cannot find module './models/modela'

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:20 25 4
gpt4 key购买 nike

我的应用程序有问题,文件树是:

│   app.js
│ package-lock.json
│ package.json

├───models
│ modela.js
│ seedDB.js

└───views
│ index.ejs
│ landing.ejs
│ new.ejs
│ show.ejs

└───partials
footer.ejs
header.ejs

我在 Mongoose 模型中遇到错误

internal/modules/cjs/loader.js:800
throw err;
^

Error: Cannot find module './models/modela'
Require stack:
- E:\Project\models\seedDB.js
- E:\Project\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (E:\project\models\model-b.js:2:20)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (E:\project\app.js:6:14)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'E:\\project\\models\\seed.js',
'E:\\project\\app.js'
]
}

我的 app.js 文件包含 -

const express  = require('express'),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
projectItem = require("./models/modela");
seedDB = require("./models/seedDB");

seedDB();

模型组成

var mongoose   = require("mongoose");
var projectItemSchema = new mongoose.Schema({
name: String,
image: String,
description: String
});
module.exports = mongoose.model("projectItem",projectItemSchema)

seedDB 组成

const mongoose   = require("mongoose");
const Campground = require("./models/modela");

function seedDB() {
projectItem.remove({}, function(err){
if(err){
console.log(err);
}
console.log("all projectItem removed");
});
}

module.exports = seedDB;

谁能帮帮我..当我在 app.js (启动文件)中编写所有内容时,没有错误应用程序运行良好..当我制作单独的架构模型文件时遇到此问题..

最佳答案

在您的 seedDB 文件中,您有

const Campground = require("./models/modela");

seedDBmodela 位于同一文件夹中,但您正尝试从 ./models/modela 导入它。因此,将上面的代码行替换为:

const Campground = require("./modela");

关于node.js - 错误: Cannot find module './models/modela' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59514963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com