39

I'm getting an error when I run karma start:

$ karma start
INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [preprocess]: Can not load "ng-html2js", it is not registered!
  Perhaps you are missing some plugin?

...

But in my package file I have "karma-ng-html2js-preprocessor": "*", and the folder with code for this preprocessor exists in node_modules.

Any ideas on how to solve the problem?

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
Ph0en1x
  • 9,943
  • 8
  • 48
  • 97

4 Answers4

51

In my cases, the problem was connected to lack of karma-ng-html2js-preprocessor inside karma config plugins sections. In tutorials I saw that you don't need to add 'ng-html2js' inside plugins, but for me it doesn't work without it.

Danger14
  • 760
  • 2
  • 12
  • 33
Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
23

If you are starting and running a global install of Karma, one that's installed with -g flag, and is run without specifying a path, i.e. karma start path/to/config.js, then make sure the plugins are also globally installed, i.e. npm install -g karma-ng-html2js-preprocessor.

If you're running a local install of Karma, i.e. path/to/karma start path/to/config.js then make sure plugin is also installed locally to that application.

M.K. Safi
  • 6,560
  • 10
  • 40
  • 58
  • yes, plugins installed locally for application. And I execute `karma run` from the root of application. – Ph0en1x Sep 28 '13 at 16:59
  • I think when you do `karma run` from root, you're actually running the global install of karma. Do you have it globally installed as well? – M.K. Safi Sep 28 '13 at 17:07
  • 1
    no, i'm sure i using local versions because of references in webstorm – Ph0en1x Sep 28 '13 at 18:01
  • Try to install everything globally, karma and all of its plugins. See if that helps. – M.K. Safi Sep 28 '13 at 18:06
  • Also, make sure your karma-config.js file has something like `preprocessors: {'tpl/*.html': 'ng-html2js'}`. – M.K. Safi Sep 29 '13 at 12:02
  • In order to use a local copy of karma, I put `"test": "karma start",` in the script section in my package.json – zjk May 10 '16 at 03:30
10

Check out Loading Plugins in the docs.

If you omit the plugins property, it'll try to load all the plugins that are:

  1. Prefixed with karma-.
  2. A sibling to the karma npm module.

So if your file structure is:

- node_modules
  - karma
  - karma-chrome-launcher
  - karma-firefox-launcher

...since karma-chrome-launcher and karma-firefox-launcher are siblings to the karma module that is in use, and since they both start with karma- they'll be loaded automatically.


But be careful - if you do have the plugins property defined, it'll only load the things that are defined. Ie. if you have plugins: ['karma-chrome-launcher'], it won't load karma-firefox-launcher.


Some questions to ask yourself:

  1. Are you using a local version of karma or a global version?
  2. Is everything up to date? If not try npm update or npm uninstall -> npm install.
  3. Do you have global versions of karma-x that are overriding the local ones?
Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
1

I've seen too many different types of answers about this. And I had to try to out all the different methods to finally understand what was going on. I didn't have a plugins: section in my karma.conf.js either.

So I have a project AwesomeKarmaTests which contains all the files inside inside a folder of the same name.

AwesomeKarmaTests
\package.json
\karma.conf.js
\node_modules
            \karma
            \karma-jasmine
            ...

The node_modules directory is obviously going to be created when you cd AwesomeKarmaTests and run npm install.

npm would then go through the contents of the package.json in the folder from which it was invoked and install all the packages listed and further dependencies if required.

Contents of my package.json

{
  "name": "AwesomeKarmaTests",
  "version": "1.0.0",      
  "devDependencies": {
    "karma": "^1.6.0",
    "karma-jasmine": "^1.1.0",
    "karma-junit-reporter": "^1.2.0",
    "karma-ng-html2js-preprocessor": "^1.0.0",
    "karma-phantomjs-launcher": "^1.0.4"
  }
}

I encountered this problem (Can not load "ng-html2js") initially because karma was installed globally in my system. The global karma installation would try to look for karma-ng-html2js-preprocessor package globally, and it would obviously fail to find it as I hadn't installed the pre-processor globally.

And I couldn't understand what was going on as every single one of the devDependencies in the package.json was already installed in the node_modules directory inside the root folder of the project. I kept running karma start from AwesomeKarmaTests folder without realizing that it was the global installation of karma that was being executed (silly me).

However after uninstalling karma from my global installation I started getting /c/Users/fastasticUser/AppData/Roaming/npm/karma: No such file or directory errors. That was when I realized my mistake.

Then I changed my approach. I started running my tests using the following command, explicitly specifying the path of the local installation of karma.

./node_modules/karma/bin/karma start karma.conf.js from AwesomeKarmaTests directory. And as mentioned in several other posts, karma did pick its sibling packages and plugins.

So remember to use the local versions of the karma package when relying on other locally installed plugins or packages, else karma will have trouble identifying what you want it to do.