It’s nice to be able to use yarn instead of npm on a CI server, but often you need root access to npm install -g yarn. Here is a bash script you can use, to install yarn (if not present) and then run “yarn”:
#!/bin/bash
# Choose which version of yarn you want to useEXPECTED_YARN_VERSION=**"0.16.1"
function install_yarn** {mkdir -p .yarnDOWNLOAD_URL="https://github.com/yarnpkg/yarn/releases/download/v$EXPECTED_YARN_VERSION/yarn-v$EXPECTED_YARN_VERSION.tar.gz" echo "Downloading from $DOWNLOAD_URL" curl -fL $DOWNLOAD_URL > .yarn/yarn.tar.gz tar zxf .yarn/yarn.tar.gz --strip-components=1 -C .yarn}
if [ -f .yarn/bin/yarn ]; then YARN_VERSION=$(node -e 'const fs = require("fs"); console.log(JSON.parse(fs.readFileSync(".yarn/package.json")).version);')if [ "$YARN_VERSION" != "$EXPECTED_YARN_VERSION" ]; then echo "The yarn version is $YARN_VERSION, expected $EXPECTED_YARN_VERSION. Re-downloading" rm -rf .yarninstall_yarn fielse echo "The file .yarn/bin/yarn does not exist, installing yarn"._install_yarn_**fi
**./.yarn/bin/yarn
Good luck!
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.
To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!