index.js 754 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var path = require('path');
  3. var resolveFrom = require('resolve-from');
  4. var callerPath = require('caller-path');
  5. module.exports = function (moduleId) {
  6. if (typeof moduleId !== 'string') {
  7. throw new TypeError('Expected a string');
  8. }
  9. var filePath = resolveFrom(path.dirname(callerPath()), moduleId);
  10. // delete itself from module parent
  11. if (require.cache[filePath] && require.cache[filePath].parent) {
  12. var i = require.cache[filePath].parent.children.length;
  13. while (i--) {
  14. if (require.cache[filePath].parent.children[i].id === filePath) {
  15. require.cache[filePath].parent.children.splice(i, 1);
  16. }
  17. }
  18. }
  19. // delete module from cache
  20. delete require.cache[filePath];
  21. // return fresh module
  22. return require(filePath);
  23. };