4

I'm trying to check a Node project for missing or unnecessary dependencies, so I've tried depcheck and npm-check, but they both fail in the same way, claiming all five dependencies are unnecessary when in fact they are all used, and as far as I can see, they are used in the normal way; package.json bin points to ayane.js which contains require directives for the dependencies. I tried specifying --ignore-bin-package=false to depcheck, but that made no difference. The project https://github.com/russellw/ayane is pretty small and simple, and doesn't do anything weird.

What am I missing? Both the programs in question are quite widely used; are there known problems with them?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • could you show any error message? and to your question, yes it's common that npm fails. – King Reload Apr 21 '17 at 12:15
  • @KingReload No error message per se, just the normal message saying all five dependencies were unused. Common that npm fails - can you expand on that? – rwallace Apr 22 '17 at 12:30

2 Answers2

3

In fact, depcheck seems to not appreciate the return statement outside of a function.
If you run depcheck --json, you can see depcheck considers ayane.js as an invalidFiles:

"invalidFiles":{"ayane/ayane.js":"SyntaxError: 'return' outside of function (62:1) ...

I'm not an expert on that and I don't often use return outside of a function so I am not sure if it is bad practice or not, especially in this case (and still remain curious about that).

For more information, here's the MDN error reference: SyntaxError: return not in function

You can try to replace this line in the source code of the project (to console.log('exit') for example) and try to relaunch depcheck: you'll get no more "Unused dependencies" warning but a "get-stdin Missing dependencies", and it's right.

// ayane.js l.61
if (!commander.lang && !commander.args.length && tty.isatty(process.stdin.fd))
    return

If you want, you can open an issue as a false alert in the depcheck repository.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
TGrif
  • 5,725
  • 9
  • 31
  • 52
  • That's not happening for me. `depcheck --json` gives a record starting with `{"dependencies"...`. Maybe `return` outside a function is a recently added feature. Try upgrading your version of `depcheck`? – rwallace Apr 23 '17 at 18:16
  • 2
    `depcheck --version` 0.6.7 Please look carefully the output of `depcheck --json`. There is an _invalidFiles_ entry at some point. It is not a "feature", it's a bug in ayane.js who made _depcheck_ not work correctly, and not check dependencies in an invalid file. Did you try what I suggest (replacing the return statement) ? – TGrif Apr 23 '17 at 18:34
  • You're right, there was an invalid files indicator in the middle of that record, and when I take out the global return, it now works. Thanks! – rwallace Apr 25 '17 at 00:57
2

Else you could maybe try the following steps:

  1. npm install dependency-check -g
  2. dependency-check <package.json file or module folder path>

Example: dependency-check ./package.json

You could check the following site for more info about it: dependecy-check

If it doesn't work, check for any path problems. Might be the case if it still doesn't work and doesn't show the message: Success! All dependencies used in the code are listed in package.json

If you're not sure if everything has been installed correctly, I would recommend a reinstall of the npm module. I made a reinstall solution for mac before: solution

Community
  • 1
  • 1
King Reload
  • 2,780
  • 1
  • 17
  • 42
  • Thanks! This does indeed do one half the job, checking for missing dependencies. I'm still hoping for a program that does the other half, checking for unnecessary dependencies. – rwallace Apr 22 '17 at 12:34
  • No problem ✌ and maybe you don't have unnecessary dependencies? You could also do `npm prune` to unbuild modules that aren't listed in the `package.json`, and you could check `npm help prune` – King Reload Apr 22 '17 at 12:43
  • Did you try `npm prune`? You could also use `npm install cleanup-dependencies` . To use this go to the directory you want to clean up and run the command `clean-deps`. – King Reload Apr 24 '17 at 07:06
  • Thanks, `npm prune` does indeed look like the appropriate command for what it does, though what I'm looking for is something different. – rwallace Apr 25 '17 at 01:00