Skip to content
Related Articles

Related Articles

Improve Article

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...



My Personal Notes arrow_drop_up
Recommended Articles
Page :