<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fernando López on Medium]]></title>
        <description><![CDATA[Stories by Fernando López on Medium]]></description>
        <link>https://medium.com/@ferlopezm94?source=rss-b0ad972a2413------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7owt8F9uBiNWrL9auMBoRw.jpeg</url>
            <title>Stories by Fernando López on Medium</title>
            <link>https://medium.com/@ferlopezm94?source=rss-b0ad972a2413------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 15:49:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ferlopezm94/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A quick introduction to Yarn]]></title>
            <link>https://medium.com/@ferlopezm94/a-quick-introduction-to-yarn-a5be2fb541d5?source=rss-b0ad972a2413------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5be2fb541d5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[package-manager]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Fernando López]]></dc:creator>
            <pubDate>Wed, 08 Mar 2017 01:02:02 GMT</pubDate>
            <atom:updated>2017-03-08T01:02:02.659Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tjaaP47awuadHZTQuDBrow.jpeg" /></figure><p><a href="https://yarnpkg.com/en/">Yarn</a> is a new package manager that is much faster, reliable and secure than <a href="https://www.npmjs.com/">npm</a>.</p><p>Here you’ll see an introduction to the important commands of Yarn and how you can start using Yarn in a project created with npm.</p><h3>Why Yarn?</h3><p>Facebook has used npm for developing products for a while, but once their products and teams started to grow,<a href="https://code.facebook.com/posts/1840075619545360"> they experienced problems</a> related to consistency, security and performance.</p><p>Instead of creating a solution for every new problem, they decided to tackle this problem with another approach.</p><p>Developed by Facebook in collaboration with Google, Exponent and Tilde, Yarn aims to enhance the development of products by removing the problems caused due to version incompatibility in modules used in the projects.</p><p>Some advantages that Yarn gives with respect to npm are:</p><ul><li>Much faster installation because every installed package is stored in a local cache</li><li>Enhanced security to verify the integrity of every installed package</li><li>Deterministic because the same dependencies will be installed in the same exact way no matter the environment</li><li>More intuitive commands</li></ul><p>All of this with 100% compatibility with npm, meaning that you don’t have to modify your source code in npm projects in order to switch to Yarn. How awesome!</p><h3>Installing Yarn</h3><p>In order to access Yarn from the terminal, you have to install it globally. There are <a href="https://yarnpkg.com/en/docs/install#mac-tab">different ways</a> for doing so, but here we’ll use npm</p><pre>npm install -g yarn</pre><p>Once the installation is done, we’ll create the folder for the Yarn project</p><pre>mkdir yarn-project<br>cd yarn-project</pre><p>The process for creating a Yarn project is similar to npm and the command used is init</p><pre>yarn init</pre><p>Next, a series of questions will appear and the responses will be used to configure package.json file</p><p>Here we’ll use the default values that appear in parentheses, but you can always modify these values according to your project</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*HGvvhO0HBHzF7Vp2q7xpZA.png" /><figcaption>init command result</figcaption></figure><p>Once this process is finished, package.json will be created in the root of the folder.</p><h3>A basic server</h3><p>For this post, we’ll create a basic express server.</p><p>To do that, a file for the server is needed</p><pre>touch server.js</pre><p>And the code for the server is</p><pre>// server.js</pre><pre>const express = require(&#39;express&#39;);<br>const port = process.env.PORT || 3000;<br>let app = express();</pre><pre>app.get(&#39;/&#39;, (req, res) =&gt; {<br>  res.send(&#39;Hello Yarn!&#39;);<br>});</pre><pre>app.listen(port, () =&gt; {<br>  console.log(`Server is up on port ${port}!`);<br>});</pre><p>Something important to note here is that we use the same require sentence to import modules no matter if you’re using npm or Yarn.</p><p>In fact, all the code remains the same.</p><h3>Dependencies</h3><p>Now, how could we add express as a dependency for the project?</p><p>With npm we used to run</p><pre>npm install express --save</pre><p>With Yarn, we run</p><pre>yarn add express</pre><p>You don’t have to specify --save and that’s because Yarn has better defaults.</p><p>By default, every dependency installed will update package.json.</p><p>If you prefer, you can install a specific version for a module with</p><pre>yarn add express@3.1.0</pre><p>If you don’t specify the version, the latest one will be installed.</p><p>Once express is installed, the node_modules folder will be created with the same contents of an npm project.</p><p>Indeed you can see how package.json got updated without having to indicate Yarn to do it explicitly</p><pre>// package.json</pre><pre>{<br>  &quot;name&quot;: &quot;yarn-project&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.14.1&quot;<br>  }<br>}</pre><p>But there’s one difference, though, a new file has been created: yarn.lock.</p><p>yarn.lock is a file generated automatically and you DON’T have to edit it manually.</p><p>Every time you execute Yarn commands, this file will be updated automatically.</p><p>If you are using version control, it is important not to ignore this file. Instead, you have to put it on your repo and commit its changes.</p><p>The objective of yarn.lock is to ensure that you are using the same exact versions of your dependencies no matter the environment like Heroku or AWS.</p><p>The content of yarn.lock is similar to npm_modules</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iIj9dI5l11FGlVDYNIkfDQ.png" /><figcaption>yarn.lock file</figcaption></figure><p>yarn.lock specifies the exact version of a package, not a range.</p><p>So even that in package.json we have ranges</p><pre>&quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.14.1&quot;<br>  }</pre><p>The content of yarn.lock is used to determine which versions to install.</p><h3>Dev dependencies</h3><p>Yarn allows you to specify dev dependencies too, for example for testing you might need mocha.</p><p>To install it you run</p><pre>yarn add mocha --dev</pre><p>And package.json has a new section</p><pre>&quot;devDependencies&quot;: {<br>    &quot;mocha&quot;: &quot;^3.2.0&quot;<br> }</pre><h3>Remove dependencies</h3><p>You can remove dependencies with remove command.</p><p>For example, to remove mocha just run</p><pre>yarn remove mocha</pre><p>This command will remove the package, and all of the packages it depends on, and update package.json and yarn.lock.</p><h3>Change versions</h3><p>Yarn allows you to change the versions of your packages, either if you want to upgrade or install a previous version.</p><p>For example, to change express from 4.14.1 to 3.1.0, you run</p><pre>yarn upgrade express@3.1.0</pre><p>After the command is executed, yarn.lock is updated.</p><p>yarn.lock is updated every time you add, upgrade or remove a package.</p><h3>Dependencies version state</h3><p>Yarn has a useful command to verify new versions for your dependencies with</p><pre>yarn outdated</pre><p>This command will analyze package.json and will display a report for every dependency.</p><p>The result returned having express@3.1.0 is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*2ZgucPLjuZU7rCy-XtlUKg.png" /><figcaption>yarn outdated result</figcaption></figure><p>Here you can see that the most recent version is 4.14.1.</p><p>If you’d like to upgrade express to the most recent version, just run (without specifying a version)</p><pre>yarn upgrade express</pre><h3>Installing all your modules</h3><p>When you download a Yarn project without its node_modules folder, in order to install all the modules you have to run</p><pre>yarn install</pre><p>With the information in yarn.lock, Yarn will install all the necessary modules.</p><p>To prove this, just remove node_modules of the current project and run the install command.</p><p>In my case, the installation time took 2.48 s as compared to 6.82 the first time round.</p><p>This difference in time is because Yarn uses a local cache.</p><p>This feature makes Yarn faster than npm. If you want more detailed about this difference I recommend you to read <a href="https://medium.freecodecamp.com/npm-vs-yarn-benchmark-9b456de4aa96#.520cijoyy">npm vs Yarn benchmark</a>.</p><h3>Local cache?</h3><p>When you first installed express@4.14.1, Yarn not only installed it in your project, it also installed it in a local cache.</p><p>Doing this, the next time you need express@4.14.1 either on the actual project or in other places, Yarn will not have to install it from the web, it will get it from the local cache.</p><p>This means that you can install packages without having an internet connection because the packages are installed locally.</p><p>Whenever you want to clean the cache, just run</p><pre>yarn cache clean</pre><h3>Yarn scripts</h3><p>Npm allows you to execute scripts specified in package.json. With Yarn, you can do it the same way.</p><p>To do that, you must add scripts property on package.json, like in npm</p><pre>&quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;&quot;,<br>    &quot;start&quot;: &quot;node server.js&quot;<br>}</pre><p>To execute one of these scripts, run</p><pre>yarn run</pre><p>After that, a list of all the available scripts you can run will appear and a prompt to choose one</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*4fJVLibmPsmLRpaFUheRAA.png" /><figcaption>yarn run result</figcaption></figure><p>You can also specify the script right away with</p><pre>yarn run start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*e_i3ToPDN-09fU9a9EOvFQ.png" /><figcaption>yarn run start result</figcaption></figure><h3>Global modules</h3><p>If you want to install global modules, you can do it with global.</p><p>For example, if you want to add nodemon globally just run</p><pre>yarn global add nodemon</pre><p>global is useful too to remove or upgrade global modules</p><pre>yarn global upgrade nodemon<br>yarn global remove nodemon</pre><h3>Switch an npm project to Yarn</h3><p>The process is very easy. To prove it, an npm project will be needed: <a href="https://github.com/ferlopez94/ReactTimer/tree/npm-project">ReactTimer</a>.</p><p>To download the project just run</p><pre>git clone git@github.com:ferlopez94/ReactTimer.git</pre><p>And navigate to the npm version of the project</p><pre>cd ReactTimer<br>git co npm-project</pre><p>Now you need to install the modules, but using Yarn</p><pre>yarn install</pre><p>After Yarn installed all the modules correctly, you have to ensure everything works the way it has to.</p><p>To check this, install and run <a href="https://webpack.github.io/">webpack</a> to bundle the files</p><pre>yarn global add webpack@1.12.13<br>webpack</pre><p>And run the server</p><pre>yarn run start</pre><p>Visit localhost:3000 in your browser and…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VOm736NKl7ynaPXkSvPzKQ.png" /><figcaption>Web app using Yarn</figcaption></figure><p>All works correctly with the modules installed with Yarn.</p><p>And because the modules were installed with Yarn, the yarn.lock file was created.</p><p>To end the process of converting an npm project to a Yarn project you have to add yarn.lock to the repo</p><pre>git add .<br>git commit -m &#39;Switch to Yarn&#39;</pre><p>And that’s all it takes to switch to Yarn.</p><h3>Conclusion</h3><p>Yarn offers a lot of new and enhanced features, but here you only saw the basics. If you want to go deeper, visit its <a href="https://yarnpkg.com/lang/en/">website</a> to learn more.</p><p>I hope you find this post useful and be able to start using Yarn in your projects to enhance your workflow.</p><p>If you liked this post, I’d appreciate it if you click in the ❤ below for more people on Medium to enjoy this. Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5be2fb541d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>