Yves Schelpe
Yves Schelpe

Reputation: 3463

Multiple export classes from same require(jsfile)

I'm currently designing an application and have some classes I'm extending. Below is a brief sample code of how it looks today, as well as where I want to go (aka one file, multiple classes and export them + consume them in main.js - or any other)..


TODAY

main.js

const UrgentTask = require('./urgentTask');
const Task = require('./task');
var ut = new UrgentTask({ name: "Some task", priority: "URGENT" });
var nt = new Task({ name: "Normal task" });

console.log(ut.toString());
console.log(nt.toString());

task.js

'use strict'

class Task {
    constructor(data) {
        this.name = data.name;
        this.completed = false;
    }

    complete() {
        console.log('completing task: ' + this.name);
        this.completed = true;
    }

    save() {
        console.log('Saving task: ' + this.name);
    }

    toString() {
        return this.name + ' ' + this.completed;
    }
}

module.exports = Task;

urgentTask.js

'use strict' 

var Task = require('./task');

// Decorate "TASK"
class UrgentTask extends Task {
    constructor(data) {
        super(data);
        this.priority = data.priority;
    }

    toString() {
        return `[${this.priority}] ${super.toString()}`;
    }
}

module.exports = UrgentTask;

WHAT I WOULD LIKE

main.js

const { Task, UrgentTask } = require('./task');
var ut = new UrgentTask({ name: "Some task", priority: "URGENT" });
var nt = new Task({ name: "Normal task" });

console.log(ut.toString());
console.log(nt.toString());

task.js

=> this would ideally export the two classes, but I don't know how to? What I already tried:

module.exports = Task;
module.exports = UrgentTask;

But this blows up in node.

Upvotes: 1

Views: 513

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074238

WHAT I WOULD LIKE

const { Task, UrgentTask } = require('./task');

That's a destructuring assignment, which assigns object properties to discrete bindings (constants, in this case).

So you need to export an object:

ES2015+ syntax (but using Node modules, not ES2015 modules):

module.exports = {Task, UrgentTask};

ES5 and earlier syntax:

module.exports = {Task: Task, UrgentTask: UrgentTask};

Actually, by default, exports already is an object so you can also do this:

module.exports.Task = Task;
module.exports.UrgentTask = UrgentTask;

But replacing it with a new object is fine too.

Simple non-Require example:

function getMyStuff() {
  class Task { }
  class UrgentTask extends Task { }
  return {Task, UrgentTask};
}

const {Task, UrgentTask} = getMyStuff();
const ut = new UrgentTask();
console.log(ut instanceof UrgentTask); // true
console.log(ut instanceof Task);       // true

NodeJS example:

mymodule.js:

class Task { }
class UrgentTask extends Task { }
module.exports = {Task, UrgentTask};

myapp.js:

const {Task, UrgentTask} = require("./mymodule.js");
const ut = new UrgentTask();
console.log(ut instanceof UrgentTask); // true
console.log(ut instanceof Task);       // true

Upvotes: 2

Related Questions