Sunday, 9 April 2017

Is there a shorthand way to "require or download" for NodeJS?

I'm trying to create code that will intelligently find a way to run itself, no questions asked, instead of complaining about missing dependencies. What I really want is something like

var aes256 = require_or_install('nodejs-aes256');

that equates to

var aes256 = null;
try {
  aes256 = require('nodejs-aes256');
} catch(e) {
  const exec = require('child_process').exec;
  console.log('nodejs-aes256 not found. I\'ll fix that for you.');
  exec('npm install --save nodejs-aes256');
  aes256 = require('nodejs-aes256');
}



via wuxiekeji

No comments:

Post a Comment