node package manager


karma-ng-json2js-preprocessor Build Status

Preprocessor for converting JSON files to AngularJS constants.


The easiest way is to keep karma-ng-json2js-preprocessor as a devDependency in your package.json. You can simple do it by:

npm install karma-ng-json2js-preprocessor --save-dev


// karma.conf.js 
module.exports = function(config) {
    preprocessors: {
      '**/*.html': ['ng-html2js'],
      '**/*.json': ['ng-json2js']
    plugins: [
    files: [
      'app/**/*.js',         // application files 
      'test/fixture/*.json', // JSON fixtures 
      'test/spec/*.js',      // test files 
    ngJson2JsPreprocessor: {
      // strip this from the file path 
      stripPrefix: 'test/fixture/',
      // prepend this to the 
      prependPrefix: 'served/',
      /* or define a custom transform function
      cacheIdFromPath: function(filepath) {
        return cacheId;

How does it work ?

This preprocessor converts JSON files into Angular constants and puts them in separate Angular modules; each named the same as the source JSON file and generates Angular modules.

For instance this test/fixture/data.json ...

    "prop": "val"

... with the configuration given above will be converted into:

angular.module('served/data.json', []).constant('servedData', {
    prop: 'val'

Inject json fixture into your test case:

describe('me', function(){
    it('should not fail', function() {
        var testFixture;
        inject(function (_servedData_) {
            testFixture = _servedData_;
            prop: 'val'

Browser support

This package is tested against the following browsers:

  • Firefox, Chrome, Edge & Safari: latest version
  • IE 9-11
  • Chrome for Android: latest version
  • Android 4.1+
  • iOS: latest two versions

Other browsers and versions might work but there's no guarantee.

Automated tests are possible due to the courtesy of BrowserStack.

BrowserStack icon

Supported Node.js versions

This project aims to support all Node.js LTS versions in the "active" phase (see LTS README for more details) as well as the latest stable Node.js.


Before sending a pull request, run grunt in terminal to make sure all tests pass. To continuously run tests during development, run karma start.

For more information on Karma see the homepage.