{"id":6980,"date":"2019-05-10T21:45:51","date_gmt":"2019-05-10T21:45:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6980"},"modified":"2019-06-11T13:51:20","modified_gmt":"2019-06-11T13:51:20","slug":"starting-out-with-yarn","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/starting-out-with-yarn\/","title":{"rendered":"Starting out with Yarn"},"content":{"rendered":"<p>Yarn is a node package manager that can be used in place of npm. Beyond the basic package management functionality, such as adding and removing dependencies it can also execute scripts to create applications, such as React apps. and build them (i.e. get them ready for deployment) as well as run them.<\/p>\n<p><strong>Creating package.json<\/strong><\/p>\n<p>To get started we would run the following command within the project&#8217;s folder to generate a package.json file (or create one yourself using your preferred text editor). <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn init\r\n<\/pre>\n<p>You&#8217;ll be presented with a set of questions which will then result in the contents of the package.json file. These include your project name, version, description, git repository etc. If you don&#8217;t have any response to the questions just press enter, we can always fill in missing data later.<\/p>\n<p>Here&#8217;s an example package.json file<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;name&quot;: &quot;MyProject&quot;,\r\n  &quot;version&quot;: &quot;1.0.0&quot;,\r\n  &quot;description&quot;: &quot;MyProject Description&quot;,\r\n  &quot;main&quot;: &quot;index.js&quot;,\r\n  &quot;repository&quot;: {\r\n     &quot;url&quot;: &quot;https:\/\/myproject-repos&quot;,\r\n     &quot;type&quot;: &quot;git&quot;\r\n  },\r\n  &quot;author&quot;: &quot;PutridParrot &lt;emailaddress&gt;&quot;,\r\n  &quot;license&quot;: &quot;MIT&quot;\r\n}\r\n<\/pre>\n<p><strong>Adding dependencies<\/strong><\/p>\n<p>I said at the start of this post that yarn is a package manager, so obviously we&#8217;ll want to add some package dependencies. Again, we can add our information to the package.json directly using our preferred text editor or use the yarn application from the command line to do this. Ofcourse the yarn command does a little more, it will check the yarn registry (by default this is https:\/\/registry.yarnpkg.com\/)<\/p>\n<p>To add a package dependency we use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn add &#x5B;package]\r\n<\/pre>\n<p>So for example, we can add React using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn add react\r\n<\/pre>\n<p>This results in the addition for the following to the package.json<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  &quot;dependencies&quot;: {\r\n    &quot;react&quot;: &quot;^16.8.6&quot;\r\n  }\r\n<\/pre>\n<p>Check out <a href=\"https:\/\/yarnpkg.com\/en\/packages\" rel=\"noopener noreferrer\" target=\"_blank\">Packages<\/a> to allow us to search for packages.<\/p>\n<p>We can remove a package using <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn remove react\r\n<\/pre>\n<p><strong>Creating an application<\/strong><\/p>\n<p>We can also create an application. For example to create a React application (i.e. grabs the dependencies, generates the code etc.) we can use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn create react-app my-app\r\nyarn create react-app my-app --typescript\r\n<\/pre>\n<p><strong>Running scripts<\/strong><\/p>\n<p>We can also add scripts to the package.json, like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n    &quot;start&quot;: &quot;react-scripts start&quot;,\r\n    &quot;build&quot;: &quot;react-scripts build&quot;,\r\n    &quot;test&quot;: &quot;react-scripts test&quot;,\r\n    &quot;eject&quot;: &quot;react-scripts eject&quot;\r\n  }\r\n<\/pre>\n<p>Now we can execute scripts using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nyarn start\r\n<\/pre>\n<p>There&#8217;s much more yarn can do, but this post covers the most often used commands (or at least the one&#8217;s I&#8217;ve used most so far).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yarn is a node package manager that can be used in place of npm. Beyond the basic package management functionality, such as adding and removing dependencies it can also execute scripts to create applications, such as React apps. and build them (i.e. get them ready for deployment) as well as run them. Creating package.json To [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[242],"tags":[],"class_list":["post-6980","post","type-post","status-publish","format-standard","hentry","category-yarn"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6980"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6980\/revisions"}],"predecessor-version":[{"id":7165,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6980\/revisions\/7165"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}