在Node.js中找不到模块

时间:2019-01-25 04:57:02

标签: node.js mongodb

这是app.js中的我的require模块

 var Comment = require('/models/comments');

我创建了一个名为modules的文件夹,然后将我的架构插入到名为comments.js的文件中。

 var mongoose = require('mongoose');

 var CommentSchema = mongoose.Schema({
  image: {
  type:String,
   unique : true
 },
 comment: {
   type: String,
 }



 });

  var Comment = module.exports = mongoose.model('Comment',CommentSchema);
   module.exports.createComment = function(newComment,callback){
        newComment.save(callback);
  }

我收到此错误

    Error: Cannot find module '/models/comments'

这是我的文件部分

enter image description here

1 个答案:

答案 0 :(得分:0)

require('/models/comments')指向文件系统的根。
要从app.js(基于您的屏幕截图)中找到它,请将其更改为:require('./models/comments')

相关问题