Major Despard
Major Despard

Reputation: 95

How can I export a class from one file to another file in Node?

Imagine I have this class:

class Class {
    constructor(arg1, arg2) { arg1 = arg2};
}

Should I do this?

class Class = exports.Class {
    constructor(arg1, arg2) { arg1 = arg2};
}

Or there's another way?

Upvotes: 0

Views: 184

Answers (2)

Eduardo Junior
Eduardo Junior

Reputation: 350

You should do like this (for other ways, check @Snow answer):

class Class {
    constructor(arg1, arg2) { arg1 = arg2};
}

module.exports = Class;

Upvotes: 2

Snow
Snow

Reputation: 4097

With export syntax, just put export before the class:

export class Class {

(this results in a named export named Class)

Or, for a default export:

export default class Class {

With module syntax, assign to module.exports, or to a property of module.exports:

module.exports = class Class {

or

module.exports.Class = class Class {

Upvotes: 2

Related Questions