How to export class with static methods in Node.js ?
Last Updated :
26 May, 2021
We know JS static keyword defines static properties and methods for a class. Static method or properties can not be called from instances of the class. Instead, they're calling from the class itself.
class Car {
static run() { console.log('Car running...') }
}
// Error: (intermediate value).run
// is not a function
( new Car() ).run()
// Car running...
Car.run()
Relation between require() and module.exports: By default, module.exports point to an object. The value of module.exports can be literal, function, object, etc. When we export a module, it means we export the value of module.exports.
The task of require() function is to import the value of module.exports in the module from where it was called. The value returned by the require() function in moduleB is equal to the module.exports object in the moduleA. So, the Relation is shown below.
require() == module.exports
Export class with static method: If you want to export a class with a static method from NodeJS module then the idea is as simple as exporting a class itself. Let's see some examples.
Step 1: Create a NodeJS project using the following command.
mkdir Project && cd Project
npm init -y
Step 2: Create two JS files in the root directory of your project and name them App.js and myModule.js
Project Structure: It will look like this.

Step 3: Edit myModule.js with the following code.
myModule.js
// myModule module
class Car{
static run() { console.log('Car running...') }
}
// Export this module
module.exports = Car
We have created a class called Car with static method run() in the myModule.js and export it by assigning the class itself to module.exports
Step 4: Next, edit your App.js with the following code.
App.js
// Import myModule
const mercedes = require('./myModule')
// Printing data
console.log(mercedes)
// Invoke static function
mercedes.run()
In the App.js file, we import the myModule using require() function and assign the return value to the variable name mercedes. So, the class Car directly assigns to mercedes, and now we can invoke the static method run() from mercedes from App.js module.
Step 5: Run the server using the following command.
node App.js
Output:
[Function: Car]
Car running...
If, you want to export a module in a form of an object then, add class Car as a property of module.exports as shown below:
myModule.js
// myModule module
class Car{
static run() { console.log('Car running...') }
}
// Export this module
// add class Car as a property
// of module.exports
module.exports = { Car }
App.js
// Importing our module
const mercedes = require('./myModule').Car
// Executing run function
mercedes.run()
Run the server using the following command.
node App.js
Output:
Car running...
Similar Reads
How to write code using module.exports in Node.js ? module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to
3 min read
How to use Routes with serve-static Files in Node.js ? Using serve-static files with routes in a Node.js application involves creating a server with Express, defining routes, and serving static files like HTML, CSS, JavaScript, images, etc. Hereâs an article explaining how to use serve-static with routes in Node.js.Serving Static Files with Routes in No
3 min read
How to use EcmaScript Modules in Node.js ? Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
2 min read
How to have path alias in Node.js ? Node.js has tons of modules that can be very hard to deal with. Thus, it's a good practice to have a neat and well-defined directory structure. In this article, we are going to deal with a problem that involves the Node.js modules. Â Let's first understand what is the need to have path alias. There a
3 min read
How to do Templating using ExpressJS in Node.js ? Template Engine : A template engine basically helps us to use the static template files with minimal code. At runtime, the template engine replaces all the variables with actual values at the client-side. Templating Engine Examples: EJS (Embedded JavaScript Templating) Pug Mustache In this article w
2 min read
How To Create Modules in NodeJS? Modules are the building blocks of NodeJS code. They help you break down your project into smaller, manageable pieces, each with its own job.To create a module, just make a JavaScript file and export what you want to share. Other files can then import and use those exports, adding that functionality
3 min read