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

444
Views
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
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!