1

I have created the following two classes in my classes.js:

class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    display() {
        console.log(this.firstName + " " + this.lastName);
    }
}

module.exports = {
    Person
};

As you can see I am exporting the two classes via module.exports.

Person = require('./classes.js');

const someone1 = new Person("First name", "Last name"); // <-- does NOT work

const someone = new Person.Person("First name", "Last name"); // does work
someone.display();

However, when calling the classes I get an error, when calling the class directly.

Any suggestions how to call the class directly?

I appreciate your replies!

2
  • 2
    module.exports = Person; If you have two classes, well, you want either different files or to do something like Person = require('../classes.js').Person; Commented Apr 15, 2019 at 9:30
  • Hint: You're exporting an object { Person: Person } from the file. Commented Apr 15, 2019 at 9:34

4 Answers 4

3

If you want all your classes in one file called classes.js, then this should work;

// classes.js

class Person {
  // ...
}

class Vehicle {
  // ...
}

module.exports = {
  Person,
  Vehicle
}
// some-other-file.js

const { Person } = require('../classes')

const person = new Person('First', 'Last')

Although to keep things easy to understand, my recommendation would be to split your class into multiple files and then export each class from its file directly;

class Person {
  // ...
}

module.exports = Person
const Person = require('../classes/person')

// do something
Sign up to request clarification or add additional context in comments.

Comments

1

In this case you're exporting an object containing Person class

module.exports = {
    Person
};

So when you import like Person = require('./classes.js')

You're actually importing the exported object. So Person after you've imported is similar to

Person = {
  Person
};

You can either change the export by assigning the desired export object to module.exports like:

module.exports = Person

or change your import to use destructuring assignment to import like (notice the curly braces):

const { Person } = require('./classes.js');

Comments

1

If

module.exports = {
    Person
};

therefore

Person = require('./classes.js').Person;

Alternatively, you can

module.exports = Person;

and

Person = require('./classes.js');

Comments

1

In classes.js you are exporting Person as an object property. So to make it work as you expect you can either do Destructuring Assignment

const { Person } = require('./classes')

Or just require class directly from classes.js file.

const Person = require('./classes').Person

Alternatively, and it's a better option, in my opinion, you can separate Person to its own file and export it by default.

File ./Person.js

class Person {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    display() {
        console.log(this.firstName + " " + this.lastName);
    }
}

module.exports = Person

And then in your main file just do:

const Person = require('./Person');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.