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

448
Vistas
Dynamic require() in pkg (node.js)

IMPORTANT: If you want to help me or you have the same issue discuss it here: https://github.com/vercel/pkg/discussions/1580

I'm writing a node.js app which downloads repositories from GitHub at runtime. These contain javascript files which I require at runtime. But now I want to package my code into an executable using pkg, but I read that I can't just simply require packages at runtime using pkg. So my question is: How can I require these files at runtime? I'ts important for me to access its exported function, as I need to pass variables into it. Here is an example dynamically downloaded file:

module.exports = function PluginServerSideRenderer (app, dir, config) {
    /*
     *
     * app is the express app
     * dir is the current-working-directory of the server or mostly the root
     * config is the config.json file as a javascript object
     * 
     */

    app.get('/__plugin-template__', (req, res) => {
        // Uncomment if user must be logged in to access this page
        // if (req.session.loggedin !== true) return res.redirect('/login') // Login Check
        res.send('Hello World from the Plugin-Template!')
    })
}

Normally I would use this code to import these files:

// Plugin Server Side Renderer
const plugindirx = path.join(dir, '.tvt', 'plugins')
const files = fs.readdirSync(plugindirx)
const activeplugins = JSON.parse(fs.readFileSync(path.join(plugindirx, 'active.json'), 'utf8').toString())

for (const plugindiroutoffiles of files) {
   if (plugindiroutoffiles === 'active.json') continue
   if (!(activeplugins[genFileName(plugindiroutoffiles)])) continue

   const filesx = fs.readdirSync(path.join(plugindirx, plugindiroutoffiles))
   const ssrfile = path.join(plugindirx, plugindiroutoffiles, filesx[0], 'serverside.js')

   require(ssrfile)(app, dir, config, Logger, IServConfig)
}
// End Plugin Server Side Renderer
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