Can you export multiple classes from a single Nodejs Module?
Solution 1
You can export multiple classes like this:
e.g. People.js
class Jack{
//Member variables, functions, etc
}
class John{
//Member variables, functions, etc
}
module.exports = {
Jack : Jack,
John : John
}
And access these classes as you have correctly mentioned:
var People = require('./People.js');
var JackInstance = new People.Jack();
var JohnInstance = new People.John();
Solution 2
You can also do this in a shorter form, using destructuring assignments (which are supported natively starting from Node.js v6.0.0):
// people.js
class Jack {
// ...
}
class John {
// ...
}
module.exports = { Jack, John }
Importing:
// index.js
const { Jack, John } = require('./people.js');
Or even like this if you want aliased require assignments:
// index.js
const {
Jack: personJack, John: personJohn,
} = require('./people.js');
In the latter case personJack
and personJohn
will reference your classes.
A word of warning:
Destructuring could be dangerous in sense that it's prone to producing unexpected errors. It's relatively easy to forget curly brackets on export
or to accidentally include them on require
.
Node.js 12 update:
Lately ECMAScript Modules received an extended support in Node.js 12.*, introducing the convenient usage of import
statement to accomplish the same task (currently Node should be started with a flag --experimental-modules
in order to make them available).
// people.mjs
export class Jack {
// ...
}
export class John {
// ...
}
Notice that files that adhere to modules convention should have an .mjs
extension.
// index.mjs
import {
Jack as personJack, John as personJohn,
} from './people.mjs';
This is much better in a sense of robustness and stability, as an attempt to import
non-existing export
from the module will throw an exception similar to:
SyntaxError: The requested module 'x' does not provide an export named 'y'
Native Coder
Programmer, open-source enthusiast, and father of two.
Updated on July 25, 2022Comments
-
Native Coder almost 2 years
Currently, I have 4 Child Classes each in their own file. I'm requiring them all in the same file. I am wondering if I can contain all 4 of those classes in a single module. Currently, I'm importing them like this
var Jack = require('./Jack.js'); var JackInstance = new Jack(); var Jones = require('./Jones.js'); var JonesInstance = new Jones();
I'd like to import them like this
var People = require('./People.js'); var JackInstance = new People.Jack();
Or even
var Jack = require('./People.js').Jack; var JackInstance = new Jack();
My classes are defined like so
class Jack{ //Memeber variables, functions, etc } module.exports = Jack;
-
Native Coder over 7 yearsREALLY!? I tried this exact method earlier! Let me try again. I'll accept your ans asap.
-
Native Coder over 7 yearsAs it turns out, I just had a typo. Thanks!!
-
Shashi about 7 years@MukeshSharma: I want class Jack and John will be in a separate file like jack.js and john.js...then will require people.js and want to access jack instance using people.jack. could you please help?
-
Ridhwaan Shakeel about 3 yearswould destructuring work?
var {John, Jack} = require('./People.js'); var JackInstance = new Jack();