简体   繁体   中英

Cannot find module 'models/PermissionModel' error when I try to require my javascript file

I created a folder called models under my node server project inside it a javascript file called PermissionModel.js

   var express = require('express');
var mongoose = require('mongoose');
var Schema=mongoose.Schema,
Objectid=Schema.Objectid;

var permissionSchema=new mongoose.Schema({
        docType:'string',
        permission:'array'

});
var PermissionModel=mongoose.model('permission',permissionSchema);

module.exports=PermissionModel;

then I try to import it inside my server.js file

var PermissionModel=require('models/PermissionModel');

when try to run my node project this error occured

Cannot find module 'models/PermissionModel'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/sankha/Documents/Projects/HealthApp/blu_kross_server/server.js:4:21)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

please help me to solve this thank you very much!!

Change your require line to:

var PermissionModel=require('./models/PermissionModel');

If you don't put the . before the model, Node will look for the module only inside node_modules folder.

For further information read the docs of node, you can also ask google and find this

inside your models directory create a file index.js then export PermissionModel.js in your index.js file

exports.PermissionModel = require('./PermissionModel');

inside your server.js import the whole directory as below

var PermissionModel=require('./models');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM