D
D
Daniil Chashkov2019-03-13 10:00:21
Node.js
Daniil Chashkov, 2019-03-13 10:00:21

Module.exports does not produce what is required, how to fix it?

There is server.js:

var User = require('./user');
function run() {
  var vasya = new User("Вася");
  var petya = new User("Петя");

  vasya.hello(petya);
}

if (module.parent) {
  exports.run = run;
} else {
  run();
}

There is user/index.js:
var phrases = require('./ru');

function User(name){
    this.name = name;
}

User.prototype.hello = function(who){
    console.log(phrases.Hello + ", " + who.name);
};
console.log(module);
module.exports = User;

After executing server.js in the console, it displays an error:
var vasya = new User("Vasya");
^
TypeError: User is not a constructor
at run (/home/daniil/MyProjects/nodeJS-learn/server.js:3:15)
at Object. (/home/daniil/MyProjects/nodeJS-learn/server.js:12:3)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal /modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
at startup (internal/bootstrap/node. js:303:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)

What am I doing wrong?
It seems like in module.exports = User, which means that in User in the server.js file, there should be User from index.js, but there is an object with User, and not the User itself. For some reason, module.exports returns a { User } object, not a User.
Ps version node 11

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Chashkov, 2019-03-13
@imnotwhoexpect

own inattention. Issue resolved!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question