<?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: 5inchiRoca</title>
    <description>The latest articles on DEV Community by 5inchiRoca (@5inchiroca).</description>
    <link>https://dev.to/5inchiroca</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%2F2945754%2F1f963740-aa19-4c0b-a3fe-55a3a4adf3e2.png</url>
      <title>DEV Community: 5inchiRoca</title>
      <link>https://dev.to/5inchiroca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/5inchiroca"/>
    <language>en</language>
    <item>
      <title>NestJs + Yarn v4: Installation and deployment</title>
      <dc:creator>5inchiRoca</dc:creator>
      <pubDate>Sat, 15 Mar 2025 23:21:13 +0000</pubDate>
      <link>https://dev.to/5inchiroca/nestjs-yarn-v4-installation-and-deployment-kjd</link>
      <guid>https://dev.to/5inchiroca/nestjs-yarn-v4-installation-and-deployment-kjd</guid>
      <description>&lt;p&gt;I encountered issues installing NestJS with Yarn v4, such as missing dependencies and CLI errors. I searched for this specific issue but couldn’t find an exact match, so I decided to write this post&lt;/p&gt;

&lt;p&gt;Tech used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node v22&lt;/li&gt;
&lt;li&gt;Yarn v4&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing NestJS with Yarn v4
&lt;/h2&gt;

&lt;p&gt;We need to use &lt;code&gt;@nestjs/schematics&lt;/code&gt; otherwise it will throw an error like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Error: Collection &lt;span class="s2"&gt;"@nestjs/schematics"&lt;/span&gt; cannot be resolved.

Failed to execute &lt;span class="nb"&gt;command&lt;/span&gt;: node @nestjs/schematics:application &lt;span class="nt"&gt;--name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;project-name-2 &lt;span class="nt"&gt;--directory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;undefined &lt;span class="nt"&gt;--no-dry-run&lt;/span&gt; &lt;span class="nt"&gt;--no-skip-git&lt;/span&gt; &lt;span class="nt"&gt;--no-strict&lt;/span&gt; &lt;span class="nt"&gt;--package-manager&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"yarn"&lt;/span&gt; &lt;span class="nt"&gt;--collection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"@nestjs/schematics"&lt;/span&gt; &lt;span class="nt"&gt;--language&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"ts"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this, we include &lt;code&gt;@nestjs/schematics&lt;/code&gt; in our command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn dlx &lt;span class="nt"&gt;-p&lt;/span&gt; @nestjs/schematics &lt;span class="nt"&gt;-p&lt;/span&gt; @nestjs/cli nest new project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We still need to install dependencies&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="nb"&gt;cd &lt;/span&gt;project-name
yarn &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I recommend setting Yarn on your project using&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This will update our &lt;code&gt;package.json&lt;/code&gt; so in the next step using &lt;code&gt;corepack enable&lt;/code&gt; will automatically use the Yarn version defined on our &lt;code&gt;package.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default, Yarn v4 uses Plug’n’Play (PnP), which creates &lt;code&gt;.pnp.*&lt;/code&gt; files. These files should not be committed, so we add them to &lt;code&gt;.gitignore&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/.yarn
.pnp.*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;p&gt;This is the &lt;code&gt;Dockerfile&lt;/code&gt; I created&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;node:22-alpine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /build&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;corepack &lt;span class="nb"&gt;enable&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build

&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:22-alpine&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json yarn.lock .&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;corepack &lt;span class="nb"&gt;enable&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; yarn workspaces focus &lt;span class="nt"&gt;--production&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /build/dist dist&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 80&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ "yarn", "start:prod" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the &lt;code&gt;.dockerignore&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.yarn
.pnp.*
dist
*.md
.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn workspaces focus &lt;span class="nt"&gt;--production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;replicates the old&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--production&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will need to make some adjustments based on your project needs. We don't need to copy the &lt;code&gt;.yarn&lt;/code&gt; directory from the &lt;code&gt;builder&lt;/code&gt; stage, since Yarn uses global cache. The command above creates a &lt;code&gt;.yarn&lt;/code&gt; directory with production-only dependencies inside &lt;code&gt;/app&lt;/code&gt; directory as well as the default global cache folder for the current stage. We can see the path of this cache folder using the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn config get cacheFolder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup ensures a smooth installation of NestJS with Yarn v4 and provides an optimized &lt;code&gt;Dockerfile&lt;/code&gt; for production deployments. Let me know in the comments if you have any questions or improvements!&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>yarn</category>
    </item>
  </channel>
</rss>
