Valerie
Valerie

Reputation: 229

Including Custom Modules in Node Express

I'm new to Node/Express and I'm not entirely sure where things go...

I want to have a little custom class - where do I put this custom code in my express app? I seem to have to put it inside "node_modules" for it to be picked up by require which isn't really what I want. Ideally I'd like to have it in a lib folder or the likes.

How can I do this?

Upvotes: 2

Views: 1673

Answers (1)

Ash
Ash

Reputation: 6783

Let's say you had a Person class like this in lib/person:

var Person = function (firstName) {
  this.firstName = firstName;
};

You can export this using node's module.exports like this (in lib/person):

module.exports = Person;

To use the person class you would then do:

var Person = require('./lib/person');   
var jim = new Person('jim');

Upvotes: 1

Related Questions