<?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: tevez07b9</title>
    <description>The latest articles on DEV Community by tevez07b9 (@tevez07b9).</description>
    <link>https://dev.to/tevez07b9</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F663401%2Fecb18e26-c772-46a1-8480-215f83f5be21.png</url>
      <title>DEV Community: tevez07b9</title>
      <link>https://dev.to/tevez07b9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tevez07b9"/>
    <language>en</language>
    <item>
      <title>Managing monorepos with lerna and yarn workspaces</title>
      <dc:creator>tevez07b9</dc:creator>
      <pubDate>Thu, 15 Jul 2021 05:14:02 +0000</pubDate>
      <link>https://dev.to/tevez07b9/managing-monorepos-with-lerna-and-yarn-workspaces-4bhl</link>
      <guid>https://dev.to/tevez07b9/managing-monorepos-with-lerna-and-yarn-workspaces-4bhl</guid>
      <description>&lt;p&gt;Reusing code is a good thing, splitting your project into multiple small packages could be helpful, its' easier to focus when working on teams. It's always better to split the problem into smaller pieces. &lt;/p&gt;

&lt;p&gt;But when it comes to managing these packages, it rapidly turns into a nightmare, things are dependent on one another, updating multiple packages and maintaing their separate repos.&lt;/p&gt;

&lt;p&gt;That's where monorepos come in. You can have multiple packages in a single repository. They all can share the same node_modules folder. You can easily make them dependent on one another, and publish them all at once. No need of managing versions, lerna will take care of that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Ftevez07b9%2Fcourscate%2Fraw%2Fmaster%2F7c868c27-9989-48c7-b6b5-b108f9416743_text.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Ftevez07b9%2Fcourscate%2Fraw%2Fmaster%2F7c868c27-9989-48c7-b6b5-b108f9416743_text.gif" alt="Cool huh, gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;Initialize your project with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This should create your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;You must have &lt;code&gt;workspaces&lt;/code&gt; here set to the folders where you have your packages created. Note the &lt;code&gt;version&lt;/code&gt; here, it's &lt;code&gt;1.0.0&lt;/code&gt; at start, but as you progess further, lerna is gonna update it according. &lt;/p&gt;

&lt;p&gt;A good pattern is to use the &lt;code&gt;conventionalCommits&lt;/code&gt; config with lerna to manage versions. You can read more about that &lt;a href="https://www.conventionalcommits.org/en/v1.0.0/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eg. &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Next run &lt;code&gt;lerna init&lt;/code&gt;, you can combine the &lt;code&gt;--independent&lt;/code&gt; flag if you wanna maintain versions of each package separately.&lt;/p&gt;

&lt;p&gt;Eg. &lt;code&gt;lerna.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;useWorkspaces&lt;/code&gt; option enables us to reuse the setting for Yarn Workspaces as Lerna's workspace setting.&lt;br&gt;
You can switch the &lt;code&gt;npmClient&lt;/code&gt; option from &lt;code&gt;yarn&lt;/code&gt; to &lt;code&gt;npm&lt;/code&gt;, if you want &lt;code&gt;npm&lt;/code&gt; to run all your commands. Finally &lt;code&gt;commands&lt;/code&gt; option is set to use &lt;code&gt;conventionalCommits&lt;/code&gt; for our publish process.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding packages
&lt;/h3&gt;

&lt;p&gt;You can create a packages folder and start adding creating your packages there or use this command &lt;code&gt;$ npx lerna create @projectName/packagename&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding npm packages as dependencies
&lt;/h3&gt;

&lt;p&gt;With yarn workspaces you don't need to cd to each packages folder in order to install dependcies, all you gotta do is &lt;code&gt;$ yarn workspace @projectName/yourpackagename add npm-package-name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you want to install shared dependencies, &lt;code&gt;$ yarn add -W --dev typescript prettier eslint&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Pretty slick, right?😎&lt;/p&gt;
&lt;h3&gt;
  
  
  Publishing
&lt;/h3&gt;

&lt;p&gt;Once you have coded your packages in the respective directories, now is the time to publish them. &lt;code&gt;lerna publish&lt;/code&gt; is the command used to publish your packges. &lt;/p&gt;

&lt;p&gt;If you used the &lt;a href="https://www.conventionalcommits.org/en/v1.0.0/" rel="noopener noreferrer"&gt;Conventional Commits&lt;/a&gt; pattern, you can use the &lt;code&gt;--conventional-commits&lt;/code&gt; flag with the above command to determine the version of your package. With this option, Lerna generates a &lt;code&gt;CHANGELOG.md&lt;/code&gt; for a new version.&lt;/p&gt;

&lt;p&gt;Here is an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npx lerna publish --conventional-commits
lerna notice cli v3.19.0
lerna info versioning independent
lerna info Looking for changed packages since @zoomify/rest-api-client@1.0.0
lerna info getChangelogConfig Successfully resolved preset "conventional-changelog-angular"

Changes:
    - @zoomify/rest-api-client: 1.0.0 =&amp;gt; 1.1.0
    - @zoomify/cutomize-uploader: 3.0.1 =&amp;gt; 3.0.2

? Are you sure you want to publish these packages? (ynH)
:
Successfully published:
    - @zoomify/rest-api-client@1.1.0
    - @zoomify/customize-uploader@3.0.2
lerna success published 2 packages
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Run npm-scripts in multiple packages
&lt;/h3&gt;

&lt;p&gt;Even with the handy yarn workspaces commands, it could be frustating to test and build each and every package, if you wanna run a command across all pacakges, just do this,&lt;/p&gt;

&lt;p&gt;Eg, let's say i wanna build all my packages before publishing.&lt;br&gt;
&lt;code&gt;$ npx lerna run build --stream&lt;/code&gt;, this will call the build command specified in &lt;code&gt;package.json&lt;/code&gt; files of each package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Ftevez07b9%2Fcourscate%2Fraw%2Fmaster%2Ftenor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Ftevez07b9%2Fcourscate%2Fraw%2Fmaster%2Ftenor.gif" alt="that's cool"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
