简体   繁体   中英

NodeJs : modules.export : Cannot find one of my own custom module

I am trying to include my own users.js module to my router file. It keeps throwing the error:

Cannot find module './router/users.js'

My directory structure is as follows:

nodejs (Main folder on my drive)

-- expressserver.js (My server file)

-- package.json

-- router (folder containing main.js router and users.js user details file)

----- main.js

----- users.js

----- orders.js

Here my users module is in the same folder as my router (main.js)

My code for router is:

var url = require('url');
var users = require('./router/users.js');

module.exports = function (app) {

    app.get('/', function (req, res) {
        res.render('index.html');
        console.log("Home page displayed");
    });

app.get('/login', function (req, res) {
        res.render('login.html');
        console.log("Log in page displayed");

    });

app.get('/api/orders/:id', function (req, res) {
      console.log(req.params.id); 
      res.json(ORDER.orders[req.params.id]);
    });

    app.get('/api/orders/', function (req, res) {
      console.log(ORDER); 
      res.json(ORDER);

    });    
 app.get('/api/users/:id', function (req, res) {
      console.log(req.params.id); 
      res.json(USERS.users[req.params.id]);
    });

    app.get('/api/users/', function (req, res) {
      console.log(USERS); 
      res.json(USERS);

    }); 

My code for users.js:

module.exports = function () {

// Create User prototype and objects
var USERS = { users: [] };

function User(type, useremail, password) {
 this.type = type;
 this.useremail = useremail;
 this.password = password;
}

var Bob = new User("rep", "bob@bob.com", "qwerty");
USERS.users.push(Bob);

var Helen = new User("rep", "helen@helen.com", "test");
USERS.users.push(Helen);

var Dominic = new User("customer", "dom@dom.com", "1234");
USERS.users.push(Dominic);

var James = new User("grower", "james@james.com", "pass1");
USERS.users.push(James);

};

I'm pretty new to this are but have been reading up alot on modules but still can't figure it out. Any suggestions on where I have gone wrong? or what I need to do to rectify the problem?

Note: Previously I did the same thing for including router into the server file using module.exports = function (app) { around my router and in my server as: require('./router/main')(app);

Since you have specified a relative path to the module, that path is relative to the directory of the source file where require is performed. In your case, it is already relative to the 'router' directory. This will work:

var users = require('./users.js');

Or even just the following, since the extension is automatically resolved:

var users = require('./users');

The path for require() , is set for node_modules folder by default.

That is why you are able to require all modules such as var url = require('url'); in your case.

If the module is not found there in your current project, the module will be searched globally(if there are path variables set on machine).

Now when you define custom modules , you can either keep them within the folder node_modules , OR you can give a path relative to your current JS file within which you are requiring the module.

For example,

var users = require('./users');

If there is another folder, in your current working directory, say modules , then you can do require it like this:

router

----- main.js

----- orders.js

----------------modules(folder)

-----------------------users.js

var users = require('./modules/users');

So the path for require always starts from node_modules folder

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