<?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>Forem: Kamil Mysliwiec</title>
    <description>The latest articles on Forem by Kamil Mysliwiec (@kamilmysliwiec).</description>
    <link>https://forem.com/kamilmysliwiec</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%2F196704%2Ff7ee2903-3d5d-4942-8af8-81a8b332397c.png</url>
      <title>Forem: Kamil Mysliwiec</title>
      <link>https://forem.com/kamilmysliwiec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kamilmysliwiec"/>
    <language>en</language>
    <item>
      <title>Announcing NestJS Monorepos and new CLI commands</title>
      <dc:creator>Kamil Mysliwiec</dc:creator>
      <pubDate>Tue, 01 Oct 2019 12:52:25 +0000</pubDate>
      <link>https://forem.com/trilon/announcing-nestjs-monorepos-and-new-cli-commands-1n0b</link>
      <guid>https://forem.com/trilon/announcing-nestjs-monorepos-and-new-cli-commands-1n0b</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally published on the &lt;a href="https://trilon.io/blog/announcing-nestjs-monorepos-and-new-commands"&gt;Trilon Blog&lt;/a&gt; on Sep 31, 2019.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this blog post, we'll be looking at a new API of the latest Nest CLI, which supports an alternate structure for managing multiple projects and libraries in a one single repo called &lt;strong&gt;monorepo&lt;/strong&gt;. In addition, I'll give you some insights about the new CLI commands that have just been introduced, respectively &lt;code&gt;nest build&lt;/code&gt; and &lt;code&gt;nest start&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're not familiar with &lt;strong&gt;&lt;a href="https://nestjs.com"&gt;NestJS&lt;/a&gt;&lt;/strong&gt;, it is a TypeScript Node.js framework that helps you build enterprise-grade efficient and scalable Node.js applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  History
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Before we dive further, let's take a step back to see how everything was handled in the past&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Until now, we used TypeScript &lt;code&gt;tsc&lt;/code&gt; compiler by default. To provide a good developer experience in the development environment (e.g. reload the application on file change), we utilized &lt;code&gt;nodemon&lt;/code&gt;, &lt;code&gt;ts-node&lt;/code&gt; and &lt;code&gt;tsc-watch&lt;/code&gt;.&lt;br&gt;
This was perfect for most projects, but we found that some members of the community moved toward &lt;code&gt;webpack&lt;/code&gt; in combination with &lt;code&gt;ts-loader&lt;/code&gt;. That means, that eventually several packages were needed in order to handle basic features which led to various side-effects and inconsistencies.&lt;/p&gt;

&lt;p&gt;Similarly, some companies needed to follow a monorepo approach, instead of having a separate repository for every single application (or library).&lt;br&gt;
Consequently, even more libraries, tools and different packages become required.&lt;br&gt;
In order to solve this problem, we made a decision to address all these issues directly in the official CLI.   &lt;/p&gt;


&lt;h2&gt;
  
  
  Builders 🏗
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;nest build&lt;/code&gt;, you can use the same command to compile your application or library in either development or production environment.&lt;br&gt;
Would you like to watch for changes and recompile on each source file modification? Use &lt;code&gt;nest build --watch&lt;/code&gt;. Would you like to switch to &lt;a href="https://webpack.js.org/"&gt;webpack&lt;/a&gt;? Use &lt;code&gt;nest build --webpack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nPYCqkSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/f86c9b0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nPYCqkSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/f86c9b0.gif" alt="nest build"&gt;&lt;/a&gt;&lt;code&gt;nest build --watch --webpack&lt;/code&gt; example&lt;/p&gt;

&lt;p&gt;But the builder itself is not just a wrapper around the compiler (&lt;code&gt;webpack&lt;/code&gt; or &lt;code&gt;tsc&lt;/code&gt;).&lt;br&gt;
It also has plugin system that allows you to leverage the build process itself for pre or post compilation (e.g. to automatically provide additional metadata for &lt;code&gt;@nestjs/swagger&lt;/code&gt; to reduce the boilerplate). &lt;br&gt;
In fact, one plugin is already built-in into the compiler. This plugin will automatically resolve your path aliases (e.g. &lt;code&gt;@trilon/core&lt;/code&gt; imports) so you will no longer have to use helper packages like &lt;code&gt;tsconfig-paths&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;HINT:&lt;/strong&gt; Learn more about the available builder options &lt;a href="https://docs.nestjs.com/cli/usages#nest-build"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Start 🚀
&lt;/h2&gt;

&lt;p&gt;In the past you may have been confused by all of the different &lt;code&gt;package.json&lt;/code&gt; scripts in a starter application (&lt;code&gt;ts-node&lt;/code&gt;, &lt;code&gt;tsc-watch&lt;/code&gt; and &lt;code&gt;nodemon&lt;/code&gt;). &lt;br&gt;
Thanks to &lt;code&gt;nest start&lt;/code&gt; (and &lt;code&gt;nest start --watch&lt;/code&gt;), all listed packages become useless. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJLjTXtZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/8e06663.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZJLjTXtZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/8e06663.gif" alt="nest start"&gt;&lt;/a&gt;&lt;code&gt;nest start --watch&lt;/code&gt; example&lt;/p&gt;

&lt;p&gt;In the past, you may have faced an issue after adding a single TS file in the root directory of your project - and &lt;code&gt;npm run start:*&lt;/code&gt; stopped working. But rest assured - that will no longer be the case. Nest CLI will automatically detect whether your output files are located within &lt;code&gt;dist&lt;/code&gt; or &lt;code&gt;dist/src&lt;/code&gt; directory and execute appropriate entry file based on that assumption.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;HINT:&lt;/strong&gt; Learn more about the available &lt;code&gt;nest start&lt;/code&gt; options &lt;a href="https://docs.nestjs.com/cli/usages#nest-start"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Monorepos 🐱
&lt;/h2&gt;

&lt;p&gt;Over the last few years, monorepos became quite popular in the developer community. And even though there are downsides of using monorepos, the &lt;strong&gt;benefits&lt;/strong&gt; they bring provide substantial value.&lt;br&gt;
Monorepos make it easier to compose modular components and libraries, track cross-project changes, promote code re-use, and make integration testing simpler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FZqvYxDp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://trilon.io/_nuxt/img/2a088ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FZqvYxDp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://trilon.io/_nuxt/img/2a088ab.png" alt="Monorepo diagram"&gt;&lt;/a&gt;Monorepo that consist of 2 applications (payments and alerts) and 1 shared library&lt;/p&gt;
&lt;h3&gt;
  
  
  Applications and libraries
&lt;/h3&gt;

&lt;p&gt;In order to meet the demands of the community, we've added &lt;code&gt;nest g app&lt;/code&gt; and &lt;code&gt;nest g lib&lt;/code&gt; commands that allow you to convert the existing structure to a monorepo mode structure. &lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;nest g app alert-service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;will scaffold the sub-application alongside your existing application within the same &lt;a href="https://docs.nestjs.com/cli/workspaces"&gt;workspace&lt;/a&gt;. These two applications will share the same &lt;code&gt;node_modules&lt;/code&gt; folder (&lt;em&gt;single-version policy&lt;/em&gt;) and configuration files (e.g. &lt;code&gt;tsconfig.json&lt;/code&gt; and &lt;code&gt;nest-cli.json&lt;/code&gt;).&lt;br&gt;
However, these applications can be executed, developed and deployed &lt;strong&gt;separately&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lD34IAu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/6863795.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lD34IAu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/6863795.gif" alt="nest g app"&gt;&lt;/a&gt;&lt;code&gt;nest g app&lt;/code&gt; example&lt;/p&gt;

&lt;p&gt;Similarly, to generate a &lt;a href="https://docs.nestjs.com/cli/libraries"&gt;library&lt;/a&gt; (a general-purpose feature that can be used within multiple projects), you can use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;nest g lib &lt;span class="nb"&gt;users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote class="alternate"&gt; 
  Note that a library created inside a monorepo is &lt;strong&gt;not suitable&lt;/strong&gt; for publishing to the NPM registry. If you want to create a package that you want to publish / share across different repos, you should rather use &lt;code&gt;nest new&lt;/code&gt;. 
&lt;/blockquote&gt;

&lt;p&gt;Both these commands will automatically update the &lt;code&gt;nest-cli.json&lt;/code&gt; which holds the metadata needed to build and organize workspace projects. Typically though, you won't have to edit its content manually (unless you want to change the default file names etc.). You can read more about the monorepo mode &lt;a href="https://docs.nestjs.com/cli/monorepo#monorepo-mode"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building projects
&lt;/h3&gt;

&lt;p&gt;To build a single project, you can simply call &lt;code&gt;$ nest build NAME&lt;/code&gt; command where &lt;code&gt;NAME&lt;/code&gt; is the name of the application / library you passed to the &lt;code&gt;$ nest g&lt;/code&gt; command. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: In the monorepo mode, CLI will use &lt;code&gt;webpack&lt;/code&gt; instead of &lt;code&gt;tsc&lt;/code&gt; by default. The reason for this change is that &lt;code&gt;webpack&lt;/code&gt; can produce a single file bundling all project components together (resolves cross-references out-of-the-box). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Running projects
&lt;/h3&gt;

&lt;p&gt;Likewise, in order to run particular application, you can call &lt;code&gt;$ nest start NAME&lt;/code&gt; command where &lt;code&gt;NAME&lt;/code&gt; is the name of the application / library you passed to the &lt;code&gt;$ nest g&lt;/code&gt; command. &lt;/p&gt;

&lt;blockquote class="alternate"&gt; 
  However, keep in mind that libraries cannot run on their own because they don't have &lt;code&gt;main.ts&lt;/code&gt; file. 
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Generate building blocks
&lt;/h3&gt;

&lt;p&gt;If you are familiar with Nest CLI already, you know that &lt;code&gt;$ nest g&lt;/code&gt; allows you to quickly scaffold essential building blocks of your application, such as controllers and providers.&lt;br&gt;
However, what happens if you switch from single project mode to a monorepo? Nest CLI is now setup to show an &lt;em&gt;interactive list&lt;/em&gt; of all applications / library in a workspace so you can select exactly where you want something generated!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s57W9hHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/7245a8a.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s57W9hHD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://trilon.io/_nuxt/img/7245a8a.gif" alt="nest g service"&gt;&lt;/a&gt;&lt;code&gt;nest g service&lt;/code&gt; example&lt;/p&gt;

&lt;h2&gt;
  
  
  Backward compatibility
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;nest start&lt;/code&gt; and &lt;code&gt;nest build&lt;/code&gt; is &lt;strong&gt;not required&lt;/strong&gt;. All existing applications will work as expected - there are no breaking changes! New features were implemented to make developers life easier, but you can still use the same techniques as before to compile and serve your application (using either &lt;code&gt;typescript&lt;/code&gt; compiler or &lt;code&gt;webpack&lt;/code&gt; directly is totally fine).&lt;/p&gt;

&lt;h2&gt;
  
  
  In Conclusion
&lt;/h2&gt;

&lt;p&gt;With the latest CLI we've introduced many new great features for your applications.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New &lt;code&gt;build&lt;/code&gt; &amp;amp; &lt;code&gt;start&lt;/code&gt; commands&lt;/li&gt;
&lt;li&gt;Monorepo support&lt;/li&gt;
&lt;li&gt;Generate upgrades

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nest g app NAME&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nest g lib NAME&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Interactive support letting you choose exactly &lt;em&gt;where&lt;/em&gt; to generate&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We hope you're as excited about these new features as we are, and we look forward to hearing your feedback and how we can improve the NestJS ecoystem even more!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Become a Backer or Sponsor to Nest by&lt;/em&gt; &lt;a href="https://opencollective.com/nest"&gt;&lt;em&gt;donating to our open collective&lt;/em&gt;&lt;/a&gt;&lt;em&gt;. ❤&lt;/em&gt;&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>node</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
