<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stepan Vanzuriak</title>
    <description>The latest articles on DEV Community by Stepan Vanzuriak (@stepanvanzuriak).</description>
    <link>https://dev.to/stepanvanzuriak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F210495%2Fa64fbda0-6af8-4ea1-a23f-366ceea5eae1.jpeg</url>
      <title>DEV Community: Stepan Vanzuriak</title>
      <link>https://dev.to/stepanvanzuriak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stepanvanzuriak"/>
    <language>en</language>
    <item>
      <title>Let's start with Yarn 2 workspaces</title>
      <dc:creator>Stepan Vanzuriak</dc:creator>
      <pubDate>Wed, 22 Jul 2020 06:18:27 +0000</pubDate>
      <link>https://dev.to/stepanvanzuriak/let-s-start-with-yarn-2-workspaces-32bo</link>
      <guid>https://dev.to/stepanvanzuriak/let-s-start-with-yarn-2-workspaces-32bo</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;Photo by &lt;a href="https://unsplash.com/@plhnk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Paul Hanaoka&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/yarn?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A couple of days ago I found it quite difficult to figure out how to use yarn 2 and workspaces and spend hours searching for an explanation. This post is an attempt to fill the gap between docs and real-life example. &lt;/p&gt;

&lt;p&gt;Yarn 2 is different package manager then "classic" first version and workspaces were improved with behaviour and commands (more about new features here &lt;a href="https://dev.to/arcanis/introducing-yarn-2-4eh1"&gt;https://dev.to/arcanis/introducing-yarn-2-4eh1&lt;/a&gt;). &lt;/p&gt;

&lt;h3&gt;
  
  
  What's workspaces?
&lt;/h3&gt;

&lt;p&gt;From &lt;a href="https://yarnpkg.com/features/workspaces"&gt;docs&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Yarn workspaces aim to make working with &lt;a href="https://yarnpkg.com/advanced/lexicon#monorepository"&gt;monorepos&lt;/a&gt; easy, solving one of the main use cases for &lt;code&gt;yarn link&lt;/code&gt; in a more declarative way. In short, they allow multiple of your projects to live together in the same repository AND to cross-reference each other - any modification to one's source code being instantly applied to the others.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So it's a mechanism to manage your monorepos, or if simple, if your project structure similar to this you may found this useful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\project-root
 \folder-a
    package.json
 \folder-b
    package.json
 ...
 package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initialize project
&lt;/h3&gt;

&lt;p&gt;Let's define a new project with yarn 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;hello-yarn-worspaces
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ./hello-yarn-worspaces
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's init yarn here&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Use &lt;code&gt;-y&lt;/code&gt; flag to skip questions about package name, version, author, etc.&lt;/p&gt;

&lt;p&gt;Now your root package.json file should look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hello-yarn-worspaces"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MIT"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to check if yarn installed in the system, running &lt;code&gt;yarn -v&lt;/code&gt; in your terminal app should show system version, if you not sure read install part of docs &lt;a href="https://yarnpkg.com/getting-started/install"&gt;https://yarnpkg.com/getting-started/install&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To set yarn 2 as a package manager for the current project you need run two commands in &lt;strong&gt;your project root&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn policies set-version berry
&lt;span class="nv"&gt;$ &lt;/span&gt;yarn &lt;span class="nb"&gt;set &lt;/span&gt;version berry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you ready to define your sub-packages. Lets create &lt;code&gt;package-a&lt;/code&gt; folder and &lt;code&gt;package-b&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Our structure now looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\hello-yarn-workspaces
    \package-a
    \package-b
package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s run &lt;code&gt;yarn init -y&lt;/code&gt; for both folders, let’s look again to our structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\hello-yarn-worspaces
    \package-a
     package.json
    \package-b
     package.json
package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Simple code example
&lt;/h3&gt;

&lt;p&gt;We need two files, one &lt;code&gt;index.js&lt;/code&gt; to package-a and another &lt;code&gt;index.js&lt;/code&gt; to package-b&lt;/p&gt;

&lt;p&gt;&lt;code&gt;package-b/index.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;From b. You made it!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;package-a/index.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;package-b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our simple code it’s just idea to visualize linking two packages.&lt;/p&gt;

&lt;p&gt;Let's link package-b as dependencies for package-a:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;package-a/package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"package-a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"package-b"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"workspace:package-b"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After run &lt;code&gt;yarn&lt;/code&gt; command into the root folder.&lt;/p&gt;

&lt;p&gt;As final we should run &lt;code&gt;package-a/index.js&lt;/code&gt;, yarn uses non-classic linking via &lt;code&gt;.pnp.js&lt;/code&gt; &lt;strong&gt;so to run it we need to replace &lt;code&gt;node ./package-a/index.js&lt;/code&gt; command with &lt;code&gt;yarn node ./package-a/index.js&lt;/code&gt;&lt;/strong&gt; (or add “start”: “node index.js” to &lt;code&gt;package-a/package.json&lt;/code&gt;) &lt;/p&gt;

&lt;p&gt;If everything correct output will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;From b. You made it!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Yarn 2 workspaces great for multipackage repositories, follow this tutorial and you will able create own simple workspace.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>javascript</category>
      <category>yarn</category>
    </item>
  </channel>
</rss>
