<?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: eiymba</title>
    <description>The latest articles on DEV Community by eiymba (@eiymba).</description>
    <link>https://dev.to/eiymba</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%2F332549%2F84c38d57-0ab8-46db-ad32-a03193a00d02.jpg</url>
      <title>DEV Community: eiymba</title>
      <link>https://dev.to/eiymba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eiymba"/>
    <language>en</language>
    <item>
      <title>Adding Loki From DigitalOcean's Loki 1-Click Installer To Grafana In Your DigitalOcean 1-Click Prometheus Monitoring Stack</title>
      <dc:creator>eiymba</dc:creator>
      <pubDate>Sat, 18 Dec 2021 12:32:10 +0000</pubDate>
      <link>https://dev.to/eiymba/adding-loki-from-digitaloceans-loki-1-click-installer-to-grafana-in-your-digitalocean-1-click-prometheus-monitoring-stack-4f3d</link>
      <guid>https://dev.to/eiymba/adding-loki-from-digitaloceans-loki-1-click-installer-to-grafana-in-your-digitalocean-1-click-prometheus-monitoring-stack-4f3d</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;🍬 Set your Loki HTTP URL to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://loki.loki-stack.svc.cluster.local:3100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Add Loki As A Data Source&lt;/li&gt;
&lt;li&gt;How Does It Work?&lt;/li&gt;
&lt;li&gt;
Troubleshooting

&lt;ul&gt;
&lt;li&gt;502: Bad Gateway&lt;/li&gt;
&lt;li&gt;Data source connected, but no labels received&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; This guide assumes you do not have another Loki service running, and that the Loki service installed by DigitalOcean is called &lt;code&gt;loki&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A DigitalOcean account.&lt;/li&gt;
&lt;li&gt;A kubernetes cluster set up on DigitalOcean.&lt;/li&gt;
&lt;li&gt;Prometheus Monitoring Stack already installed &lt;strong&gt;as a 1-click application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Loki stack already installed &lt;strong&gt;as a 1-click application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Administrator privileges in the Grafana dashboard which exists in the &lt;code&gt;kube-prometheus-stack&lt;/code&gt; namespace.&lt;/li&gt;
&lt;li&gt;DNS resolver (comes as standard with DigitalOcean's kubernetes service).&lt;/li&gt;
&lt;li&gt;You're using the defaults of the 1-click installers.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can port forward the Grafana service using &lt;code&gt;kubectl&lt;/code&gt; &lt;em&gt;(needs to be installed independently on your local machine, and connected to your cluster)&lt;/em&gt; to access your Grafana dashboard, e.g:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl port-forward svc/kube-prometheus-stack-grafana -n kube-prometheus-stack 3000:80
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Note:&lt;/em&gt; This example assumes port &lt;code&gt;3000&lt;/code&gt; is available on your machine. If it's already in use, change the &lt;code&gt;3000:80&lt;/code&gt; part of the command to something else, followed by &lt;code&gt;:80&lt;/code&gt;, e.g. &lt;code&gt;5000:80&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can then access your Grafana dashboard by navigating to: &lt;code&gt;http://localhost:3000&lt;/code&gt; in your browser, or whatever you've set your local port as.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;DigitalOcean allows you to install a Prometheus monitoring stack using a 1-click installation option, alongside a Loki stack.&lt;/p&gt;

&lt;p&gt;The problem is, you end up with two sets of Grafana dashboards that are disjoint from each other; one in the &lt;code&gt;kube-prometheus-stack&lt;/code&gt; namespace created by the Prometheus Monitoring Stack's 1-click installer, and one in the &lt;code&gt;loki-stack&lt;/code&gt; namespace created by the Loki 1-click installer.&lt;/p&gt;

&lt;p&gt;My goal was to have the Grafana dashboard in the &lt;code&gt;kube-prometheus-stack&lt;/code&gt; display logs from the Loki service running under the &lt;code&gt;loki-stack&lt;/code&gt; namespace.&lt;/p&gt;

&lt;p&gt;Here's how I did it...&lt;/p&gt;

&lt;h2&gt;
  
  
  Add Loki As A Data Source
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;From your browser, navigate to the Grafana dashboard which exists in the &lt;code&gt;kube-prometheus-stack&lt;/code&gt; namespace (see prerequisites if you're not sure how). On the side navigation panel of the Grafana UI, hover over ⚙ &lt;code&gt;Configuration&lt;/code&gt; to reveal a sub menu. Inside this sub menu, select &lt;code&gt;Data sources&lt;/code&gt;. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dDftlc3H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htnru2w62syuci4qwgnu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dDftlc3H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/htnru2w62syuci4qwgnu.png" alt="Adding a data source" width="232" height="258"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;code&gt;Add data source&lt;/code&gt; button. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xYNaSjQw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymhup8trqitlxarncuxw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xYNaSjQw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymhup8trqitlxarncuxw.png" alt="Clicking on add data source" width="880" height="132"&gt;&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find &lt;code&gt;Loki&lt;/code&gt; as a data source and click &lt;code&gt;Select&lt;/code&gt;. &lt;em&gt;Note: You may need to search for&lt;/em&gt; &lt;code&gt;Loki&lt;/code&gt; &lt;em&gt;in the search bar to find it.&lt;/em&gt; &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--px7-31E3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ikrufd1dvquwad84lh0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--px7-31E3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ikrufd1dvquwad84lh0.png" alt="Selecting Loki as a data source" width="880" height="536"&gt;&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The only field you need to enter is the &lt;code&gt;URL&lt;/code&gt; field. Use the following value:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://loki.loki-stack.svc.cluster.local:3100
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
Afterwards, click &lt;code&gt;Save &amp;amp; test&lt;/code&gt; to apply your settings. (Didn't work?) &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sIdFSUYT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svktm5gmkbcx648d5i52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sIdFSUYT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svktm5gmkbcx648d5i52.png" alt="Loki data source configuration" width="880" height="772"&gt;&lt;/a&gt; &lt;em&gt;Note&lt;/em&gt;: If you have a previous Loki data source added, rename this new source aptly, then proceed with your chosen name for step 8.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;If you already have a dashboard created, skip this and go to step 6.&lt;/strong&gt;&lt;br&gt;
Create a new dashboard by hovering over the ➕ &lt;code&gt;Create&lt;/code&gt; menu item on the left navigation bar. In the sub menu revealed, select &lt;code&gt;Dashboard&lt;/code&gt;. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v4jANFJh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z61rwhictubboh6uj2w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v4jANFJh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z61rwhictubboh6uj2w.png" alt="Creating a new dashboard" width="204" height="148"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the &lt;code&gt;Add panel&lt;/code&gt; icon on the top right of the dashboard. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zfWUPUHp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyf72cq6vfwxo6yypb3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zfWUPUHp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyf72cq6vfwxo6yypb3u.png" alt="Add panel button" width="72" height="79"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;code&gt;Add an empty panel&lt;/code&gt;. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eYG1tFSS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xts8vd3exiuie1zk95u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eYG1tFSS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xts8vd3exiuie1zk95u.png" alt="Add an empty panel button" width="880" height="218"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure the data source to be &lt;code&gt;Loki&lt;/code&gt; (or the name given to the data source in step 4). &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eJteVMAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cznypglplb27t17kpu29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eJteVMAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cznypglplb27t17kpu29.png" alt="Data source set to Loki" width="479" height="122"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the visualisation type to change it from the default (in this case, &lt;em&gt;time series&lt;/em&gt;), to &lt;code&gt;Logs&lt;/code&gt;.&lt;br&gt;
i. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0o2vaBDr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erbrxxkfyevxnlhbebtz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0o2vaBDr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erbrxxkfyevxnlhbebtz.png" alt="Visualisation option" width="435" height="230"&gt;&lt;/a&gt;&lt;br&gt;
ii. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e5aamx-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1oqppgr192e1u8kws4p9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e5aamx-_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1oqppgr192e1u8kws4p9.png" alt="Logs option" width="397" height="262"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enter a query to read the &lt;em&gt;stdout&lt;/em&gt; of a pod, for example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{app="loki"}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--csHHtJnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aboj2tmbfph72b4tfnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--csHHtJnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aboj2tmbfph72b4tfnv.png" alt="An example query" width="880" height="577"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;(Optional)&lt;/em&gt; Consider reducing the number of data points being requested to something more modest, e.g. 100. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KCodYcrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccmjrywwn860eyuv4jv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KCodYcrQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccmjrywwn860eyuv4jv8.png" alt="Query options" width="868" height="293"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;(Optional)&lt;/em&gt; If your logs are timestamped, you may be able to group them and format the timestamp. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CKfI5ZVG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvyei4mv7gwzeafo3u3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CKfI5ZVG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvyei4mv7gwzeafo3u3m.png" alt="Log formatting options" width="394" height="495"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apply your changes. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MkYc9GWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq2nz1o13yfertwn9zjl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MkYc9GWA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq2nz1o13yfertwn9zjl.png" alt="Applying changes" width="253" height="65"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If all goes well, you should be able to see your desired logs using Loki, as installed by DigitalOcean's 1-click installer. 🥳 &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9EDuqsJn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6ezkm1kje2xyjpef9jl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9EDuqsJn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6ezkm1kje2xyjpef9jl.png" alt="A dashboard with Loki" width="880" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does It Work?
&lt;/h2&gt;

&lt;p&gt;The Loki 1-Click installer creates a service called &lt;code&gt;loki&lt;/code&gt; in the &lt;code&gt;loki-stack&lt;/code&gt; namespace, with an endpoint which the DNS resolver can interpret using the following syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;service_name&amp;gt;.&amp;lt;namespace&amp;gt;.svc.cluster.local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Loki is running inside a pod where port 3100 is being used. Thus, the complete syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;service_name&amp;gt;.&amp;lt;namespace&amp;gt;.svc.cluster.local:&amp;lt;port&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;p&gt;I'm not an expert. 🤷‍♂️&lt;/p&gt;

&lt;h3&gt;
  
  
  502: Bad Gateway
&lt;/h3&gt;

&lt;p&gt;Translation: Loki is not accessible using the URL you've provided. That probably means your installation is different to what the 1-click installer provides, or DigitalOcean has changed the way it installs Loki after this article was written.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data source connected, but no labels received
&lt;/h3&gt;

&lt;p&gt;The most likely culprits are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're pointing to the wrong service. Be sure to double check the name of the service using &lt;code&gt;kubectl get svc -n loki-stack&lt;/code&gt;, and verify that a service with the name &lt;code&gt;loki&lt;/code&gt; exists.&lt;/li&gt;
&lt;li&gt;The Loki service has a different endpoint. Verify that the endpoint with the name &lt;code&gt;loki&lt;/code&gt; exists using &lt;code&gt;kubectl get endpoints -n loki-stack&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A firewall policy. GG well met. ☠ Just kidding, but I'm no expert on kubernetes or its network policy configurations.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>cloud</category>
      <category>monitoring</category>
      <category>devops</category>
    </item>
    <item>
      <title>Compiling TypeScript to ESNext for Back-End node.js Apps</title>
      <dc:creator>eiymba</dc:creator>
      <pubDate>Sun, 26 Jul 2020 15:16:14 +0000</pubDate>
      <link>https://dev.to/eiymba/compiling-typescript-to-esnext-for-back-end-node-js-apps-190l</link>
      <guid>https://dev.to/eiymba/compiling-typescript-to-esnext-for-back-end-node-js-apps-190l</guid>
      <description>&lt;p&gt;Let's keep it short and sweet! 🍬&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;p&gt;Node Version&lt;br&gt;
Set Node To Use Modules&lt;br&gt;
TypeScript Compiler Options&lt;br&gt;
Change Your Imports&lt;br&gt;
Why Do This?&lt;/p&gt;
&lt;h2&gt;
  
  
  Node Version
&lt;/h2&gt;

&lt;p&gt;You'll need node version 13 or higher. We'll be using Node Version Manager to handle this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nvm install latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nvm ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important!&lt;/strong&gt; Note the latest version, then use it for 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;nvm use &amp;lt;version&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Replace &lt;code&gt;&amp;lt;version&amp;gt;&lt;/code&gt; with the one installed on your machine.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Proceed with caution when using the latest version over the LTS version in production.&lt;br&gt;
On Windows 10, I recommend downloading &lt;a href="https://chocolatey.org/install"&gt;chocolatey&lt;/a&gt;, then use chocolatey to install Node Version Manager; commonly referred to as "nvm".&lt;br&gt;
&lt;a href="https://github.com/nvm-sh/nvm"&gt;Click here&lt;/a&gt; for installation on Unix machines.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Set Node To Use Modules
&lt;/h2&gt;

&lt;p&gt;Add the following line to your &lt;code&gt;package.json&lt;/code&gt; file.&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="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  TypeScript Compiler Options
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;compilerOptions&lt;/code&gt; needs &lt;code&gt;target&lt;/code&gt;, &lt;code&gt;module&lt;/code&gt;, and &lt;code&gt;moduleResolution&lt;/code&gt; options to be changed in your tsconfig.json file.&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;"compilerOptions"&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;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ESNext"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ESNext"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"moduleResolution"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; remove the &lt;code&gt;resolveJsonModule&lt;/code&gt; option if you have set it. If you need to read JSON data, either use &lt;code&gt;fs.readFileSync&lt;/code&gt; at the start of your program, or utilise the new &lt;a href="https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_fs_promises_api"&gt;promise variant&lt;/a&gt;; &lt;code&gt;await fs.readFile&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Change Your Imports
&lt;/h2&gt;

&lt;p&gt;For simplicity, here are some rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All your imports must end with a &lt;code&gt;.js&lt;/code&gt; file extension, even if they end with &lt;code&gt;.ts&lt;/code&gt; in your source code. &lt;em&gt;As far as I know, &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; will pick up the correct TypeScript file.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Make sure your compiled JavaScript files and your TypeScript source files are equal in depth from your project's root folder. &lt;em&gt;For example, if your TypeScript source files are stored in &lt;code&gt;./src&lt;/code&gt; then your compiled JavaScript files should be stored in &lt;code&gt;./out&lt;/code&gt;.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;old:&lt;/strong&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;myModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/modules&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;myModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;new:&lt;/strong&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="c1"&gt;// If myModule is exported as a named function or function variable.&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;myModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/modules/myModule/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;myModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;or:&lt;/em&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="c1"&gt;// If myModule is exported as the default module.&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;myModule&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/modules/myModule/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="c1"&gt;//@ts-ignore&lt;/span&gt;
&lt;span class="nx"&gt;myModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Your file doesn't need to be called &lt;code&gt;index.js&lt;/code&gt;, but the &lt;code&gt;.js&lt;/code&gt; file extension must be present.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Done!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do This?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No more asynchronous generators!&lt;/li&gt;
&lt;li&gt;Aligns your source code closer to your compiled code.&lt;/li&gt;
&lt;li&gt;Easier to debug in production, or where source maps aren't available.&lt;/li&gt;
&lt;li&gt;You can afford to experiment with the latest APIs.&lt;/li&gt;
&lt;li&gt;Reduces dependencies (Babel and CommonJS).&lt;/li&gt;
&lt;li&gt;Performance gains.&lt;/li&gt;
&lt;li&gt;You just need a compiler which removes the types from your TypeScript files.&lt;/li&gt;
&lt;li&gt;You use the debug feature in Visual Studio Code and it already compiles for you.&lt;/li&gt;
&lt;li&gt;Your life depends on it for some unknown reason.&lt;/li&gt;
&lt;/ul&gt;

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