peerDependenciessection of the
package.jsonfile. With NPM 2, these dependencies were installed in the root
node_modulesfolder; this is NPM 3's default behaviour, and
peerDependenciesare 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
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
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
# 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
# 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!
npm shrinkwrapalso 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.