<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Docker Build fix for TypeScript]]></title><description><![CDATA[Docker Build fix for TypeScript]]></description><link>https://dockerbuildfix.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 16:28:30 GMT</lastBuildDate><atom:link href="https://dockerbuildfix.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Docker build fix for TypeScript]]></title><description><![CDATA[Issue
When building a docker image generally, we skip installing dev dependencies for production
# Skips installing dev dependencies
RUN npm ci --only=production

But when working with TypeScript you need tsc which is a dev dependecy to compile the T...]]></description><link>https://dockerbuildfix.hashnode.dev/docker-build-fix-for-typescript</link><guid isPermaLink="true">https://dockerbuildfix.hashnode.dev/docker-build-fix-for-typescript</guid><category><![CDATA[Docker]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[containerization]]></category><category><![CDATA[dependencies]]></category><category><![CDATA[devdependencies]]></category><dc:creator><![CDATA[Divy Pathak]]></dc:creator><pubDate>Sat, 11 Oct 2025 06:17:00 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-issue">Issue</h2>
<p>When building a docker image generally, we skip installing dev dependencies for production</p>
<pre><code class="lang-dockerfile"><span class="hljs-comment"># Skips installing dev dependencies</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm ci --only=production</span>
</code></pre>
<p>But when working with TypeScript you need tsc which is a dev dependecy to compile the TypeScript to JavaScript into <code>./dist</code> folder</p>
<h2 id="heading-solution">Solution</h2>
<p>Changed the Dockerfile to install all dependencies first, build, then remove dev dependecies:</p>
<pre><code class="lang-dockerfile"><span class="hljs-comment"># Install ALL dependecies (including dev dependecies for build)</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm ci</span>

<span class="hljs-comment"># Copy source code</span>
<span class="hljs-keyword">COPY</span><span class="bash"> tsconfig.json ./</span>
<span class="hljs-keyword">COPY</span><span class="bash"> src ./src</span>

<span class="hljs-comment"># Build TypeScript</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm run build</span>

<span class="hljs-comment"># Remove dev dependecies and source files after build</span>
<span class="hljs-keyword">RUN</span><span class="bash"> npm prune --production &amp;&amp; \
    rm -rf src tsconfig.json</span>
</code></pre>
<h2 id="heading-key-changes">Key Changes</h2>
<ol>
<li><p>Changed <code>npm ci —only=production</code> to <code>npm ci</code> (installs all deps)</p>
</li>
<li><p>Build happens with TypeScript available</p>
</li>
<li><p>After build, <code>npm prune —production</code> removes dev dependencies</p>
</li>
<li><p>Final image is still lean, only containing production deps and built code</p>
</li>
</ol>
<h2 id="heading-why-this-works">Why This Works</h2>
<ol>
<li><p><strong>Install phase:</strong> Gets all dependencies including TypeScript</p>
</li>
<li><p><strong>Build phase:</strong> TypeScript compiler is available</p>
</li>
<li><p><strong>Cleanup phase:</strong> Removes unnecessary dev dependencies</p>
</li>
<li><p><strong>Result:</strong> Small production image with only what's needed to run</p>
</li>
</ol>
<h2 id="heading-try-again">Try Again</h2>
<p>Run the docker build cmd again this time, it will complete successfully! ✅</p>
]]></content:encoded></item></channel></rss>