<?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: Basile</title>
    <description>The latest articles on DEV Community by Basile (@basileleroy).</description>
    <link>https://dev.to/basileleroy</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%2F611893%2Fdcf58af2-222f-451a-aaed-fb404c65c931.jpeg</url>
      <title>DEV Community: Basile</title>
      <link>https://dev.to/basileleroy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/basileleroy"/>
    <language>en</language>
    <item>
      <title>Google Analytics on React.js Components</title>
      <dc:creator>Basile</dc:creator>
      <pubDate>Wed, 05 May 2021 14:37:58 +0000</pubDate>
      <link>https://dev.to/basileleroy/google-analytics-on-react-js-components-4d2a</link>
      <guid>https://dev.to/basileleroy/google-analytics-on-react-js-components-4d2a</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Struggling to use &lt;code&gt;Google Analytics&lt;/code&gt; on a React-app?&lt;br&gt;
Do you only get data on the landing page of your app, instead of every page?     &lt;/p&gt;

&lt;p&gt;This article will be about how to fix this problem!    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The issue:
&lt;/h3&gt;

&lt;p&gt;Google Analytics (GA) sends a pageview per page load, this means that if you would go on a certain page on your website, it requests that page from the server and loads it for the user. That is what GA records and sends as data.&lt;br&gt;&lt;br&gt;
(This is in the case of a simple page view, page-events will not be discussed here!)&lt;br&gt;&lt;br&gt;
And this is also what is the issue when trying to use GA on a React-app (or any &lt;code&gt;single-page applications&lt;/code&gt;). React will load the whole app at once and switch components directly on the browser side.&lt;br&gt;&lt;br&gt;
GA doesn't see this as a new page-load as it doesn't actually load a new page, but as a reload of the same page.     &lt;/p&gt;

&lt;h3&gt;
  
  
  A solution:
&lt;/h3&gt;

&lt;p&gt;There have been different kinds of solutions to this problem, but when I went looking for one I found it confusing to find a clear and up-to-date solution. So here I am, sharing my own recommendation!     &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The idea would be that, as GA recognizes the page reload of the root URL (&lt;code&gt;yourwebsite.com/&lt;/code&gt;path/to/page) but not the URI (That is the part after the root: yourwebsite&lt;span&gt;&lt;/span&gt;.com/&lt;code&gt;path/to/page&lt;/code&gt;), the trick would be to make it check that URI everytime it reloads. And send that as the whole location of the page.    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This solution requires you to use those dependencies:     &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;react-router-dom&lt;/code&gt; and &lt;code&gt;react-ga&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm -i react-router-dom react-ga --save
or
yarn add react-router-dom react-ga --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a file in your source folder and give it a name like &lt;code&gt;useGaTracker.js&lt;/code&gt; or something, you can name it however you want though.      &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bmkdydywk7ar3jx2htp.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bmkdydywk7ar3jx2htp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 1
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go get your UA tracking code on GA.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you already have created your GA property, you can find that code like this:     &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Admin
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4z0ydy7ud2uwxeeqox3.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;Select the property you are using - and click on &lt;code&gt;Property Settings&lt;/code&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj6viccmhz5mjbbg6x0jp.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;Copy your Tracking ID (should start with UA-xxxxxxx-x)
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nggbgxs64w9hhpqvtmr.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  STEP 2
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Import &lt;code&gt;{ useEffect, useState }&lt;/code&gt; from &lt;code&gt;react&lt;/code&gt;; &lt;code&gt;{ useLocation }&lt;/code&gt; from &lt;code&gt;react-router-dom&lt;/code&gt; and &lt;code&gt;ReactGA&lt;/code&gt; from &lt;code&gt;react-ga&lt;/code&gt; inside the useGaTracker.js file.
&lt;/li&gt;
&lt;/ul&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;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&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="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&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;useLocation&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="s2"&gt;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactGA&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-ga&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  STEP 3
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's wright the function:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useGaTracker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useLocation&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;initialized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInitialized&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;ReactGA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UA-xxxxxxxxx-x&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;setInitialized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

    &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialized&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;ReactGA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pageview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;initialized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;useGaTracker&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useLocation()&lt;/code&gt; is what we will use to get our routing as location.&lt;br&gt;&lt;br&gt;
The first &lt;code&gt;useEffect()&lt;/code&gt; will check if your project is running on a localhost, if not, it will initialize your Tracking ID in the ReactGA and set the state &lt;code&gt;initialized&lt;/code&gt; to TRUE.&lt;br&gt;&lt;br&gt;
The Second &lt;code&gt;useEffect()&lt;/code&gt; will send the pageview to GA with it's location.&lt;br&gt;&lt;br&gt;
This one will also know to send that information each time &lt;code&gt;initialized&lt;/code&gt; or &lt;code&gt;location&lt;/code&gt; has changed.     &lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 4
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Import this function into our routing file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to the file that you use to route your components, and add your function in the router-component (in my case this would be my app.js file:     &lt;/p&gt;

&lt;h5&gt;
  
  
  App.js
&lt;/h5&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;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Redirect&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;react-router-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;useGaTracker&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;./useGaTracker&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nf"&gt;useGaTracker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
       &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Switch&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;exact&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Landing&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;exact&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;About&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/discord&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;exact&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Discord&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/404&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Redirect&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/404&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
       &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Switch&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;That's about it!
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should have a working connection that will save and send all the different routes you use in your routing file!&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;a href="https://basile-and-code.com/" rel="noopener noreferrer"&gt;Basile&amp;amp;Code&lt;/a&gt;
&lt;/h5&gt;

</description>
      <category>analytics</category>
      <category>google</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Choosing a Framework in Node.js</title>
      <dc:creator>Basile</dc:creator>
      <pubDate>Sat, 10 Apr 2021 14:44:26 +0000</pubDate>
      <link>https://dev.to/basileleroy/choosing-a-framework-in-node-js-3n0c</link>
      <guid>https://dev.to/basileleroy/choosing-a-framework-in-node-js-3n0c</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Today, I will be writing about my findings on using frameworks in &lt;br&gt;
a Node.js project as a beginner.    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I can only assume that every person that started coding, has been confronted with this situation...&lt;br&gt;
Where you want to achieve a certain feature or use a certain structure to ease the process when working on a project.    &lt;/p&gt;

&lt;p&gt;This post will be about my finding in this kind of situation.   &lt;/p&gt;
&lt;h4&gt;
  
  
  1. What do you want to do?
&lt;/h4&gt;

&lt;p&gt;First question to ask yourself would be what kind of features you want to have. In order to install the right framework, you will need to look for the feature you need, and the features the package will provide.&lt;br&gt;&lt;br&gt;
Some packages are specific to certain other frameworks or maybe even to certain database managers, etc ...&lt;/p&gt;

&lt;p&gt;Let's give an example:   &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://vuetifyjs.com/en/" rel="noopener noreferrer"&gt;Vuetify&lt;/a&gt;&lt;/strong&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjoczpm8jij44bg27smgp.png" alt="image"&gt;
&lt;code&gt;Vuetify&lt;/code&gt; is a framework that helps making better ui components when using &lt;code&gt;Vue&lt;/code&gt; as framework. Which means that it would not be of much use when you would be working with another type of frameworks like &lt;code&gt;Reactjs&lt;/code&gt; or &lt;code&gt;Svelte&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;
  
  
  1.2 examples of popular frameworks to use:
&lt;/h5&gt;

&lt;p&gt;When working on a Javascript project it is a popular thing to do to install both a front-end framework (which will be about the components of your website and the interface), and a back-end framework to support the server functionality of that project.     &lt;/p&gt;

&lt;p&gt;A example would be: &lt;em&gt;React.js and Express.js&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;&lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React.js&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7phdr7sg3grvd49mugqj.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7phdr7sg3grvd49mugqj.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
A React-app will provide you with a ready to use package. Which will save you a lot of time.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe33tr0kyk3aa68endk9l.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe33tr0kyk3aa68endk9l.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express.js&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqltyl7fvxhbhe59krrwb.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqltyl7fvxhbhe59krrwb.png" alt="image"&gt;&lt;/a&gt; &lt;br&gt;
On the other side, Express will be taking care of the server. like connecting an Api, Using a register/login system (can also be a framework on its own), etc...     &lt;/p&gt;

&lt;p&gt;In both parts of the project you can install specific dependencies to use. Those will be stored inside &lt;code&gt;package.json&lt;/code&gt; files. But we will come back to that later on!    &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just remember that we can use a lot of different frameworks inside a project, and even have it separated between different parts of a project, like the "front-end" part and the "back-end" part!    &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But let's continue...&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Deprecated or not?
&lt;/h4&gt;

&lt;p&gt;Let's assume that we found a framework that fits our needs for our project... It is important to look for it's activity (for example: latest update date) and usability!&lt;br&gt;&lt;br&gt;
Factors to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has it been recently updated or maintained?&lt;/li&gt;
&lt;li&gt;What are the webbrowsers that would support it?&lt;/li&gt;
&lt;li&gt;Frequently downloaded/installed?&lt;/li&gt;
&lt;li&gt;Decent and/or enough documentation?
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;As a beginner one of the biggest problems when working with a unknown framework will be figuring out how to use it.&lt;/code&gt;&lt;br&gt;&lt;br&gt;
So you might want to only use frameworks that are well documented.     &lt;/p&gt;
&lt;h4&gt;
  
  
  3. They evolve!
&lt;/h4&gt;

&lt;p&gt;Most frameworks are originally open source, which meant that it is a certainty that they will evolve regularly, unless it has been abandoned of course (but that's why we keep part 2 of this article in mind!)&lt;br&gt;&lt;br&gt;
That also means that it is my recommendation to frequently go look at the original repository for new updates/messages.    &lt;/p&gt;

&lt;p&gt;One of my favorite tips to give is to go check the "issues" page of that repository on &lt;code&gt;GitHub&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8ynpqb2192akeepl0t7.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8ynpqb2192akeepl0t7.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
It can be a goldmine of information and sometimes you may even find answers to unanswered questions about a certain error!     &lt;/p&gt;
&lt;h4&gt;
  
  
  4. The implementation
&lt;/h4&gt;

&lt;p&gt;So... We found our framework, it has what we need, is recently updated, well documented and has been used by others a lot. PERFECT!&lt;br&gt;&lt;br&gt;
Now we need to install it.     &lt;/p&gt;

&lt;p&gt;To Install a framework, we will need to initialize our node project first.&lt;br&gt;&lt;br&gt;
You can use your terminal to move into your project folder where you can use this line of code into your terminal to initialize node:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;It will ask a few configuration questions to set up your project inside a &lt;code&gt;package.json&lt;/code&gt; file.&lt;br&gt;&lt;br&gt;
After installing your first dependency (including frameworks), you will be able to find those under &lt;code&gt;"dependencies":&lt;/code&gt;, just like this:&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="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&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;"bcryptjs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.4.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cookie-parser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.4.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.8.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"debug"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~2.6.9"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"express"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~4.16.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"express-session"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.17.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"http-errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~1.6.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"morgan"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"~1.9.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"multer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.4.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"mysql"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.18.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"nodemon"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.0.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"passport"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^0.4.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"passport-local"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^1.0.0"&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;p&gt;To add new frameworks it is acutally fairly easy, you just use your package manager, for example, &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt; and use the install syntax according to the package manager you use&lt;br&gt;&lt;br&gt;
npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;path&amp;gt;to&amp;gt;project&amp;gt; npm install package-name
or...
&amp;gt;path&amp;gt;to&amp;gt;project&amp;gt; npm -i package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;path&amp;gt;to&amp;gt;project&amp;gt; yarn add package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will find out that most of the dependencies have this line of code already prepared for you to use on their website or on &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;npmjs.com&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;a href="https://basile-and-code.com/" rel="noopener noreferrer"&gt;Basile&amp;amp;Code&lt;/a&gt;
&lt;/h5&gt;

</description>
      <category>frameworks</category>
      <category>node</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
