Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues where NPM@8 needs instanciating #8

Open
hash-bang opened this issue Dec 30, 2021 · 2 comments
Open

Issues where NPM@8 needs instanciating #8

hash-bang opened this issue Dec 30, 2021 · 2 comments

Comments

@hash-bang
Copy link

There is an issue where exported npm module is a class which needs instantiating before it can be used with npm@8.3.0.

Below is a quick-and-dirty patch here where passing in the instanciate=true prop to your loader function correctly creates a singleton instance which is valid.

module.exports = (function requireNPM (npmPath, instanciate) {
  GLOBAL_NPM_PATH = npmPath
  try {
    var npm = require(npmPath)
    if (instanciate) npm = new npm();
    if (npm && Object.keys(npm).length) {
      return npm
    }
  } catch (e) {
    if (e.message.indexOf('The programmatic API was removed') >= 0) {
      // for npm >= 8. Maybe programmatic api via require('npm') is no longer available
      // https://github.com/npm/cli/pull/3762
      return requireNPM(path.join(npmPath, 'lib/npm.js'), true)
    } else if (e.code !== 'MODULE_NOT_FOUND') {
      throw e
    }
  }
  throwNotFoundError()
})(GLOBAL_NPM_PATH)

I'm not certain if this is needed in all cases though since your original patch in #7 seems to imply this was already working in npm@8? If so maybe extend the catch to have a "try to instantiate this" clause if its an event emitter class return.

@dracupid
Copy link
Owner

I have tried this solution recently, but I am not sure the returned instance is expected. npm@8 has changed a lot.

@hash-bang
Copy link
Author

Yeah unfortunately the downstream libraries that rely on it (this one for example) all break when they change how to setup the API object.

I kinda get how they need a class instantiating to use the NPM implementation but that raises the question on how it was working with NPM@8 in your last patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants