- to address issues caused by installing the same packages in different orders
- decrease install times by using previously fetched and cached results
- an offline mode
- a way to guarantee that installs determine the same results with lock files
It uses the npm registry so the packages are the same as you have been using the difference is the client you use to access and install the packages.
A lot of the commands are the same as npm commands:
npm init => yarn init
add a package
npm install <name> => yarn add [package] npm install <name>@<version> => yarn add [package]@[version] npm install <name>@<tag> => yarn add [package]@[tag]
npm install => yarn
In case you havn’t caught on the previous examples had aformat of
[npm command] => [yarn command]where
npm commandis the command for the desired outcome using npm and
[yarn command]was the command for the desired outcome using Yarn.
More commands can be found in the documentation.
To install Yarn you go to the install page. Follow the recomended install procedure for your operating system and pat yourself on the back for a job well done…
Try it Out
In the project I have I’m going to add a new package. In this case lodash because why not.
yarn add lodash
``` ├─ firstname.lastname@example.org /// /// Stuff was here but removed to require less scrolling /// └─ email@example.com Done in 39.74s. PS D:\Workspace\Blog\yarn\npm-scripts-getting-started>
Now since I did this in a project that had packages already it added a
yarn.lock that is almost 2700 lines long so I wont be posting it here. The
yarn.lock file should describe to other computers how to install the same packages and what versions so that all people working on the same project have the same experience. What this means is you shouldn’t be telling some one “it works on my machine”.
Run a Script
If you read the last few posts yo umay be wondering if switching to Yarn means having to stop using npm scripts. In a word: nope.
You can use the
run command just like with npm to run a script. Since the project I and trying Yarn out with is the same one that we had at the end of Getting Started with NPM Scripts – Linting with Standard I can use the following command:
yarn run build:js
PS D:\Workspace\Blog\yarn\npm-scripts-getting-started> yarn run build:js yarn run v0.19.1 $ babel src -d dist --presets es2015 src\app.js -> dist\app.js src\sub\sub-app.js -> dist\sub\sub-app.js Done in 1.34s.
That’s right to run scripts with Yarn it’s just
yarn run [script name]
And everything runs just fine and there should be less surprises as the project moves from machine to machine.
The logo is a kitten, I mean come on you have to like that.
So far Yarn seems like a good idea. I’m sure there are things that I’m missing but if it holds keeps most of it’s ideals in place I might want to add the dist folder to my
.gitignore since build results should be less questionable with more consistent environment setup for the build. Sound crazy? Leave a comment below or sending an email to me at firstname.lastname@example.org and let me know.