Skip to content

Instantly share code, notes, and snippets.

@edinella
Created September 15, 2013 22:45
Show Gist options
  • Select an option

  • Save edinella/6574947 to your computer and use it in GitHub Desktop.

Select an option

Save edinella/6574947 to your computer and use it in GitHub Desktop.
Node.js "require" with specific paths array
// obtém pacote nos paths específicos
var requireAt = function requireAt(packageName, paths) {
// armazena paths originais deste ambiente, preservando-os
var originalPaths = module.paths;
// define paths para este ambiente, sobreescrevendo atuais
module.paths = paths;
// obtém modulo nos paths deste beat
var pkg = require(packageName);
// restaura paths originais
module.paths = originalPaths;
// retorna o pacote desejado
return pkg;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment