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

Check whether module is actually installed #80

Closed
d4rkr00t opened this issue Nov 10, 2016 · 2 comments
Closed

Check whether module is actually installed #80

d4rkr00t opened this issue Nov 10, 2016 · 2 comments

Comments

@d4rkr00t
Copy link

Hi,

I use your great webpack plugin in my prototyping tool — Aik. And it works pretty well. And covers most of use cases except one:

If I have package.json but haven't yet installed node modules npm-install-webpack-plugin thinks that they are installed because of this check:

https://github.com/ericclemmons/npm-install-webpack-plugin/blob/master/src/installer.js#L42-L44

What do you think about checking on file system whether directory exists or not instead of checking in package.json?

I can send a pull request if you think it's a good idea :)

@insin
Copy link
Collaborator

insin commented Jan 23, 2017

The plugin is alreeady checking if node_modules/${dep} exists, but is only bailing out if it exists and is a symlink.

What if we were to replace this:

  try {
    var pkgPath = require.resolve(path.join(process.cwd(), "package.json"));
    var pkg = require(pkgPath);

    // Remove cached copy for future checks
    delete require.cache[pkgPath];
  } catch(e) {
    throw e;
  }

  var hasDep = pkg.dependencies && pkg.dependencies[dep];
  var hasDevDep = pkg.devDependencies && pkg.devDependencies[dep];

  // Bail early if we've already installed this dependency
  if (hasDep || hasDevDep) {
    return;
  }

  // Ignore linked modules
  try {
    var stats = fs.lstatSync(path.join(process.cwd(), "node_modules", dep));

    if (stats.isSymbolicLink()) {
      return;
    }
  } catch(e) {
    // Module exists in node_modules, but isn't symlinked
  }

  // Ignore NPM global modules (e.g. "path", "fs", etc.)
  try {
    var resolved = require.resolve(dep);

    // Global modules resolve to their name, not an actual path
    if (resolved.match(EXTERNAL)) {
      return;
    }
  } catch(e) {
    // Module is not resolveable
  }

With this, (using node-resolve) which just checks if the dependency is resolvable from the cwd and bails if so:

  try {
    resolve.sync(dep, {basedir: process.cwd()});
    return;
  } catch(e) {
    // Module is not resolveable
  }

@d4rkr00t
Copy link
Author

It doesn't actually check if deps exists, it checks if it is in package.json

Yeah, your solution looks good :)

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

No branches or pull requests

3 participants