npm install not installing all the packages

A few days ago I was trying to install a project’s dependencies with a simple npm install, but in the instructions I was given there was an extra step to manually install one additional package. When I checked package.json, the package was there… So, what was going on!?

After searching for a while, it turns out that a previous developer used npm shrinkwrap “to predownload the packages” (or something like that, from what he said). It turns out that this command “freezes” a list of packages with its current version, so next time you do a npm install you will have the exact same dependencies versions, even if you’re using imprecise version specification in your package.json. It does this by creating a npm-shrinkwrap.json file next to your package.json (similar to a pip freeze but it also affects npm install). The problem is that once you have this file, package.json is ignored, and even if you add a new package it will not install.

So, while this is good for reproducibility of the environments, when you need to update you package list, you also need to update npm-shrinkwrap.json file. So, the steps are:

$ npm install my_new_package --save
$ npm shrinkwrap

Or, to update:

$ npm update
$ npm shrinkwrap

If you’re dependencies are screwed up like it happened to me, the easiest way to clean up the mess is:

$ rm -rf node_modules
$ npm install
$ npm shrinkwrap


Fixing Python virtualenv on OS X

This weekend I’ve been toying with an old project I created in Django a while (~2 years) ago. The project uses virtualenv, as commonly happens in this kind of projects. The original project was created originally on a different machine, and migrated to the current one. Original machine had OS X Lion, I think… or Snow Leopard, not sure. The point is, the project had been migrated thought 4 or 5 major OS X versions… and Python was a different version from the original one used to create the original virtualenv.

So, back to the problem, trying to use Anaconda for Sublime Text, I found that the Python interpreter I was specifying in the configuration was not working. After some Googling, I found a link (I’m not able to find it again…) on how to troubleshot the configuration, which at the end it boiled down to execute the Python interpreter inside your virtualenv with the following command:

~/Virtualenvs/myvirtualenv/bin/python -c pass

In my case, the result was the folling:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: ~/Virtualenvs/myvirtualenv/bin/python
  Reason: image not found
[1]    74178 trace trap  ~/Virtualenvs/myvirtualenv/bin/python -c pass

After some Googling about this problem, I found the following post: “dyld: Library not loaded: @executable_path/../.Python” Basically: “dump the packages configuration, recreate the virtualenv, restore packages, and move everything there”. In my case, the “move everything there” was a bit tricky, and the “restore packages” was even worst, as it was using some unsupported packages which was impossible to install (no pip support, and manually install was quite time consuming…). So, I tried to do something in between, and finally I found the real difference, and the reason the interpreter was failing (notice different python version):

$ ls -la old-virtualenv
lrwxr-xr-x   1 myuser  staff      78 29 nov  2014 .Python -> /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Python

$ ls -la new-virtualenv
lrwxr-xr-x   1 myuser  staff      80  5 mai 22:42 .Python -> /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/Python

So, the real fix in my case was:

$ cd old-virtualenv
$ rm .Python
$ ln -s /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/Python .Python

Generating Repetitive CSS with Loops in LESS

Less is an amazing tool for simplifying your CSS: allows you to “extend” selectors (i.e. put selectors inside other selectors), to use mixins (i.e. a kind of “functions” that allow repeating optionally parametrized sets of CSS rules) and to use variables.

One missing feature is not having loops, for example to create a repetitive rule. Luckily enough, the language is powerful enough and there are smart people capable of taking advantage of that.

In this article I found the solution, which boils down to this:

// variables
@num-tiles-in-suit: 9;

// mixin with a guard to create a series of bamboo tiles
.makeTiles(@i) when (@i > 0) {
    @tile-value: @i - 1;
    // create the current tile rule
    .tile-@{tile-value} {
        background-image: url('/img/tiles/bamboo_@{i}.png');

    // next iteration
    .makeTiles((@i - 1));

// create bamboo tiles numbered 0-8

Finally, I also found this other article, which didn’t work, probably because it was written for an older version of Less (the article is almost two years old).

Atomically increment a limited value in MySQL and PHP

Sometimes you need to acquire a limited resource in an atomically, concurrent way. This is what semaphores are for. But when this resource is in a MySQL row, apparently there is no straightforward way of doing it.

In my case, I had a row with two fields used to limit maximum resource usage: ‘used’ and ‘limit’, both integer fields. Let’s complete it with an ‘id’, and then all the remaining information you need. To give you a more concrete example, I used this to limit the number of times you could possibly use a discount coupon.

So, while investigating the best way to do it, I considered the following options:

  • Using table locking: the problem is that, if you use a MyISAM table, this locking is done at a table level. Also, you statement gets more complicated, because you have to lock, modify and unlock.
  • Using transactions: this is not supported by MyISAM engine, and in my case I was adding this to an already existing table (not created by me, BTW…).

Finally, I came with the following method, which I think is pretty clear, clean and compatible. First of all, the query uses an atomically UPDATE statement, like the one you can found in this Stack Overflow question: Is incrementing a field in MySQL atomic?

$query = "UPDATE coupon SET used = used + 1 WHERE ID_coupon='" . $this->id . "' AND used < limit";

Now, notice the WHERE clause. It selects the row by two criteria: row id (as usual) and the condition that the 'used' field is less than the 'limit' field. So, what does mean? If the number of used coupons is less than the limit, the condition will be fulfilled and the row will be found by the database engine and updated, but if it is not it won't be found, thus the update will not happen, effectively limiting the 'used' value.

Finally, the other reason of doing this way is that MySQL returns the number of rows affected by the query. Usually, this will be 1, but if we've already reached the limit the condition will not be fulfilled and the number of affected rows will be 0. So, in PHP we can check this condition with something like this (in my case I was using PEAR DB, but you can find the equivalent statement to "affectedRows()" for MySQLi or PDO):

// Error handling omitted for brevity
$rows = $dbhm->affectedRows();
if ($rows > 0) {
  // We where able to use one coupon :D
  return true;
} else {
  // All the coupons have already been used :(
  return false;

Notice that probably this technique works on other DBs, and also can be applied to other languages than PHP.