Karma throws error: Can not load “ng-html2js”, it is not registered

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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?


回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!