简体   繁体   中英

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?

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 {

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

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

module.exports = Class;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM