0
votes

I have entities directory that store user.js file like this:

"use strict";

class User {}

module.exports = User;

and in my index.js:

var User = require("./entities/User")

but I got an error

Error: Cannot find module './entities/User'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Socket.socket.on (/home/etours/capstone-etours/index.js:154:21)
at emitTwo (events.js:106:13)
at Socket.emit (events.js:194:7)
at /home/etours/capstone-etours/node_modules/socket.io/lib/socket.js:503:12
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

3
try replacing User = require("./entities/User") with User = require("./entities/user") because its based off of file name - Gurbakhshish Singh
Check your file name. The require statement is looking for exports in a file. - jeanpier_re

3 Answers

0
votes

The require function it's case sensitive so you have to write the path exactly how you created.

if you use the U lowercase it's gonna work :D

0
votes

You should try this code:

user.js

var User = function() {};
module.exports = User;

var User = class User {   
};

module.exports.User = User;

index.js

var User = require("./user");
console.log(User);
0
votes

You can also try below code:

user.js:

var User={};
module.exports = User;

var User = class User {    
};

module.exports.User = User;

index.js:

var User = require("./user");
console.log(User);