Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
How to load scripts from files into the javascript runtime

I have written a class that can load scripts from files into the runtime and then can execute those scripts - essentially a plugin system.

class Broker {
   constructor(){

       this.requests = require("../../plugins/sync/core")
       
   }
}

The scripts will also import this class, as they extend the class's capabilities.

const { Broker } = require("../../core/Broker");

module.exports = {
    isConnectableNode: {
        minRole: 2,
        script: async (data) => {
            ...
            let node = Broker.findNode(data.nodeID); // ERROR: Broker is undefined
            ...
        },
    },...

The script is called in this function within the Broker class:

async handleRequest(handler, data, user) {
        try {
            if (this.requests[handler].minRole >= user.role) {
                const res = await this.requests[handler].script(data);
                return res;
            } else {
                return { status: 401, message: "Not authorized." };
            }
        } catch (e) {
            return { status: 400, message: "Unknown Request." };
        }
    }

What works is calling a function that sets this.requests from within handleRequest() when it is empty or not initialized.

if (!this.requests) this.loadPlugins();

Now all imports are correctly loaded and available to the scripts.

Does anybody know the reason this only works when required from the closure of the handler Function and not the constructor or really anywhere outside?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda