karma-runner / karma

Spectacular Test Runner for JavaScript
http://karma-runner.github.io
MIT License
11.95k stars 1.71k forks source link

Cannot find plugin #2524

Open thany opened 7 years ago

thany commented 7 years ago

Expected behaviour

It should be able to find installed plugins.

Actual behaviour

The reverse of "expected behaviour", innit? :) It's not able to find installed plugins.

03 01 2017 11:54:18.262:WARN [plugin]: Cannot find plugin "karma-phantomjs-launcher".
  Did you forget to install it ?
  npm install karma-phantomjs-launcher --save-dev

Environment Details

Karma version 0.13.14

frameworks: ['jasmine'],
browsers: ['PantomJS'],
plugins: [
 'karma-phantomjs-launcher',
 'karma-jasmine',
 'karma-junit-reporter'
],

Steps to reproduce the behaviour

  1. Clone Angular-seed
  2. npm i
  3. npm i --save-dev karma-phantomjs-launcher
  4. Change the browsers and plugins fields in karma.conf.js like above
  5. karma start karma.conf.js
thany commented 7 years ago

Ok, one thing I found is that somehow I magically had some modules installed in the nodejs installation directory - that's not where they're supposed to be put. Global packages normally go into the user profile.

So, trying again...

karma start karma.conf.js
module.js:472
    throw err;
    ^

Error: Cannot find module 'C:\Program Files\nodejs\node_modules\karma\bin\karma'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

That's better. At least it tells me where it cannot find whatever it needs this time.

Installing karma-cli globally doesn't help.

thany commented 7 years ago

Okay, one more update, for those who also come across this problem.

There where executables for jasmine and karma in the nodejs installation folder (again, not where they're supposed to be installed). I deleted those, removed-and-reinstalled karma-cli globally, and now it works. Or at least I'm not getting any errors trying to run karma.

So, remains one question: why or how does karma decide that a plugin isn't installed when it clearly is? Can karma please also output where it didn't find the plugin it was looking for? That would help greatly in figuring out the how&why of a problem like this.

twolfson commented 7 years ago

I think the issue is a typo in browsers. It should be PhantomJS not PantomJS =/

To answer your plugin resolution question:

By default, plugins are resolved via Node.js' require statement. There is support for pattern matching (e.g. karma-* loads all matching modules) but based on your config that isn't a problem:

https://github.com/karma-runner/karma/blob/v1.3.0/lib/plugin.js#L13-L19

Node.js' require looks for a local node_modules folder and if not found, it goes up one directory, looks again, and keeps on going until it hits / and throws an error:

https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

thany commented 7 years ago

Oops @ typo :D But that didn't cause the 'cannot find plugin' error.

Anyway, it would still help (others as well I'm sure) if karma would print out the location(s) of a plugin when it couldn't find it. Googling around, it seems to be a fairly common problem.

twolfson commented 7 years ago

I think the current message is the best we can do. As linked, it's hard to predict where the node_modules folder should be (e.g. someone can be inside of test/path/to/nested/folder and run karma which resolves from node_modules, not test/path/to/nested/folder/node_modules). If we were to output that, it would be a whole lot of confusing feedback to the user x_x

We didn't find the package, please verify it was installed at one of the following:

- /home/todd/github/project/test/path/to/nested/folder/node_modules
- /home/todd/github/project/test/path/to/nested/node_modules
- /home/todd/github/project/test/path/to/node_modules
- /home/todd/github/project/test/path/node_modules
- /home/todd/github/project/test/node_modules
- /home/todd/github/project/node_modules
- /home/todd/github/node_modules
- /home/todd/node_modules
- /home/node_modules
- /node_modules
wesleycho commented 7 years ago

I took a look at this, and what I suspect happened is that the step to run cd angular-seed was forgotten. I was not able to reproduce this from those steps, this is highly likely a user error.

thany commented 7 years ago
We didn't find the package, please verify it was installed at one of the following:

- /home/todd/github/project/test/path/to/nested/folder/node_modules (and below)

Fixed that :)

The thing is, I did not forget the cd angular-seed, honestly. I think it was trying to find the plugins in the global installation (in the user profile) or in the superglobal installation (in the nodejs program directory). Whichever the case, a message like "We didn't find the plugin here & here" would help in diagnosing what went wrong.

Because with all due respect, things like "maybe you forgot such&such" or "maybe you can try this&that" is rather unscientific guesswork, while proper logging could have prevented that.