`require.resolve ()` все еще обнаруживает модуль после удаления? - PullRequest
1 голос
/ 29 мая 2020

Это должно было быть двухчасовым заданием, которое превратилось в это чудовище.

Итак, теперь я попытался установить node-fetch, используя это, и, похоже, однажды это сработало, но я кое-что изменил чтобы сделать его более привлекательным, затем использовал npm uninstall node-fetch, и он больше не хочет устанавливать ... Может быть, require.resolve() - это просто одноразовое использование?

В частности, эта строка безумна: return require.main.require(moduleWanted);

Не совсем уверен, чем это могло быть вызвано, также приветствуются любые предлагаемые общие улучшения кода.

RigidDeps.NPMInstaller = function(moduleWanted) {
return new Promise((resolve, reject) => {
  let cp = require("child_process")
      try {
          require.resolve(moduleWanted); // Check if the module exists
      } catch (e) { // It Doesn't
          console.log(`Rigid Dependencies || Module not found -> "${moduleWanted}"...\nAttempting to Install...`); // Tell the user it's installing
          cp.execSync(`npm install --prefix "${__dirname}" ${moduleWanted}`); // Do the NPM command.
          console.log(`Rigid Dependencies || "${moduleWanted}" has been installed.`);
      }

      // The Install attempt has been made, continue...
      console.log("attempted")

      try {
          resolve("Done.")
          return require.main.require(moduleWanted);
      } catch (e) {
          console.log(e)
          console.log(`Rigid Dependencies || Failed to Install "${moduleWanted}". Run from CMD or Install Manually: "npm i --save ${moduleWanted}".`);
          process.exit(1);
      }
  });
};

RigidDeps.require = async function(moduleReq) {
  this.NPMInstaller(moduleReq).then(function(result) { // Runs the Auto-Installer
    console.log("I got this far.")

    let botPath;
    let modulePath;

        try {
          botPath = path.dirname(process.argv[1]);
          modulePath = path.join(botPath, "node_modules", moduleReq);
        } catch (e) {
          console.log(e)
        }
    console.log("Did this");

  try {
    return require.main.require(modulePath);
  } catch (e) {
    console.log(e)
  }
  }, function(error) { // Gets Auto-Installer Errors
    console.log(error);
    return;
  })
};

Заранее спасибо!

Обновление: пробовали для других модулей, на самом деле он вообще не работает, require.resolve () делает свое дело, а установщик - нет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...