home
Hero image for: How to validate Bower files.

How to validate Bower files.

By Eduardo García ● CTO | October 24th, 2014

bower logo If we want to enable people to use our libraries via Bower, we need to follow some simple steps.

  • Add a bower.json files to our project.
  • Create a release matching the info in our bower file.
  • Run bower register command to share our project.

The Problem

The problem I found is the publishing process don’t validate the bower.json, so is probable that after publishing when people try to use your library even if the system detects the library is available they get an awful error like this

bower backform#~0.0.1-alpha EMALFORMED Failed to read 

"/var/folders/bh/8fr...0gn/T/enzo/bower/backform-20375-ST4wT6/bower.json"

Additional error details:
Unexpected token n

Fortunately, there is a Node.js package to validate the bower.json before to publish.

We need to install the package with the following command.

$ npm install bower-json

Now we will use the following Node.js script to validate the bower.json file, let name this script as check_bower.js

var bowerJson = require('bower-json');

// Can also be used by simply calling bowerJson()
bowerJson.read('./bower.json', function (err, json) {
    if (err) {
        console.error('There was an error reading the file');
        console.error(err.message);
        return;
    }

    console.log('JSON: ', json);
});

To use this script execute the following command.

$ node check_bower.js
JSON:  { name: 'backform',
  main: [ 'src/backform.js' ],
  version: '0.0.1-alpha',
  homepage: 'https://github.com/AmiliaApp/backform',
  author: { name: 'Martin Drapeau', email: 'martindrapeau@gmail.com' },
  description: 
'Backform takes a Backbone model and transforms it into an HTML form',
  keywords: [ 'backbone', 'form', 'underscore' ],
  license: 'MIT',
  ignore: [ '**/.*', 'node_modules', 'bower_components', 'test',
 'tests' ],
  dependencies: { backbone: '1.0.0 - 1.1.2', 
underscore: '1.4.4 - 1.6.0' } }

You can see a bower file sample at https://github.com/enzolutions/backbone.drupal/blob/master/bower.json.

Last but not least important use the documentation about what elements must be used in a bower.json to be valid.

#Readers Contribution One of my readers Josh Habdas also recommend the tool Package.json validator to validate before to send.

I hope you found this blog entry useful.

Related Posts