Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
How do I blindly pull functional blocks from a file with node js?

I am trying to make my program drag and drop extendable. Just drop a file in the modules directory and let the program try to figure it out. So I can't be sure how many or the name of each module. For testing purposes, I just have two rollAD6 and secondOne. The code below will get me the name of everything exported for the ChatCommands file, but I am not sure how to turn that into anything actionable.

console.dir(Object.keys(require('./Modules/ChatCommands')));

console.log(typeof Object.keys(require('./Modules/ChatCommands')))

let temp = Object.keys(require('./Modules/ChatCommands'))

temp.forEach(element => {

  console.log(element)

});

example output:

[ 'rollAD6', 'secondOne' ]
object
rollAD6
secondOne

I could be going about this all wrong I am not familiar with NodeJS. Just need to turn an unknown number of objects from one file into running functions in the main.

./Modules/ChatCommands

class chatCommand {
    constructor(key, name) {
        this.key = key;
        this.name = name;
    }

    //getter
    get name() {
        return `${this.name}`
    }
    get key() {
        return `${this.key}`
    }

    //setter
    set name(name) {
        return "Once name is set it cannot be changed"
    }
    set key(key) {
        return "Once key is set it cannot be changed"
    }
}

class rollAD6 extends chatCommand {
    super(key, name, sides) {
        this.sides = sides
        this.key = key;
        this.name = name;
    }
    payload(sides) {
        console.log(`${sides} `);
        var randomNumber = Math.floor(Math.random() * sides) + 1;
        console.log(`${randomNumber} `);
        return `you rolled ${randomNumber} `;
    }
}

class secondOne extends chatCommand {
    super(key, name) {
        this.key = key;
        this.name = name;
    }
    payload() {
        return "Hello there"
    }
}

module.exports = {
    rollAD6,
    secondOne
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!