Handle peer dependencies with NPM 3

The latest major version of Node.js's package manager introduces a breaking change regarding the peerDependencies section of the package.json file. With NPM 2, these dependencies were installed in the root node_modules folder; this is NPM 3's default behaviour, and peerDependencies are no longer fetched by npm install. Instead, the tool will simply warn you if these dependencies are unmet. But what if you relied on this feature and have to support both versions?

Looking for an answer to this question, I stumbled upon this article that proposes a simple solution: duplicate peerDependencies as dependencies, which will result in installing the right stuff at the right place no matter the NPM version in use. This works, but has a few drawbacks, the most important in my eyes being the obvious code configuration duplication, which often rhymes with maintenance pain.

I therefore put up a Bash workaround, that will play nicely with Jenkins jobs and other install scripts:

# Install dependencies once, just to fetch packages with peerDependencies
npm install

# Check for current NPM version
NPM_MAJOR_VERSION=`npm -v | cut -d \. -f 1`

if [ "$NPM_MAJOR_VERSION" == "3" ]
# Turn peerDependencies into dependencies for relevant packages
sed -i 's/peerDependencies/dependencies/g' ./node_modules/package-with-peer-deps/package.json

# Install again, for real this time
npm install

# Do stuff...

Kinda dirty, but it works with no need to think about it. Feel free to discuss this solution and propose something better in the comments!

Note: using npm shrinkwrap also seems to work around this once dependencies have been correctly installed at least once, which could cancel the need to perform the above trick on a regular basis.