<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Afan Khan - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Afan Khan - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 26 May 2026 10:36:17 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/whyafan/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is the tRPC Library? Explained with a Demo Project ]]>
                </title>
                <description>
                    <![CDATA[ For a while now, I've been noticing a technology named tRPC that's cited in many modern tech stacks, including T3. But I didn't know what it was or why it had become so popular. So I began researching and learning about it. I didn't know what it mean... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-trpc/</link>
                <guid isPermaLink="false">66d45d5c3a8352b6c5a2a9f1</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Afan Khan ]]>
                </dc:creator>
                <pubDate>Thu, 11 Jul 2024 19:09:18 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/trpc-image.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>For a while now, I've been noticing a technology named <a target="_blank" href="https://trpc.io/">tRPC</a> that's cited in many modern tech stacks, including <a target="_blank" href="https://create.t3.gg/">T3</a>. But I didn't know what it was or why it had become so popular.</p>
<p>So I began researching and learning about it. I didn't know what it meant or what its purpose was. So, I dug deeper into <a target="_blank" href="https://en.wikipedia.org/wiki/Remote_procedure_call">RPC</a>, <a target="_blank" href="https://en.wikipedia.org/wiki/GRPC">gRPC</a>, and other technologies to find out.</p>
<p>I found out that tRPC is a type-safe architectural style for designing APIs. But that definition is only the tip of the iceberg.</p>
<p>In this article, I want to go deeper into the roots of this iceberg and understand what tRPC is all about. This article is an in-depth explanation of tRPC, why we need it, and how to use it.</p>
<p>Note that I'm a fellow learner writing this article. I'm exploring tRPC for the first time alongside you, based on articles I've studied. It's aimed at beginners and new learners. Let's dive in together.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ol>
<li><p>Intermediate JavaScript Knowledge.</p>
</li>
<li><p>Basic TypeScript Knowledge.</p>
</li>
<li><p>Intermediate React Knowledge.</p>
</li>
<li><p>Experience with the Fetch and REST API.</p>
</li>
<li><p>Experience with using a terminal or console.</p>
</li>
<li><p>Experience with using NPM and its commands.</p>
</li>
<li><p>Experience with using CORS and connecting front-end/back-end.</p>
</li>
<li><p>Enjoying learning something new.</p>
</li>
</ol>
<p>You can find the GitHub repository and all other resources for this article <a target="_blank" href="https://github.com/whyafan/trpc-demo">here</a>.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-what-is-trpc">What is tRPC?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-do-we-need-trpc">Why do we need tRPC?</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/p/96029b5d-38ad-4b3c-a021-661b70eb6dd3/how-to-use-trpc">How to use tRPC</a></p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/news/p/96029b5d-38ad-4b3c-a021-661b70eb6dd3/conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-what-is-trpc">What is tRPC?</h2>
<p><a target="_blank" href="https://trpc.io/">tRPC</a> is a type safe TypeScript-based library that leverages the RPC API design to process API requests and deliver a response.</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Remote_procedure_call">RPC</a> stands for Remote Procedural Call. Our tRPC builds on RPC. RPC is an architectural style to design APIs like <a target="_blank" href="https://www.ibm.com/topics/rest-apis">REST</a>. Using RPC, you get rid of the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch</a> and <a target="_blank" href="https://www.ibm.com/topics/rest-apis">REST API</a>.</p>
<p>As the name suggests, tRPC puts a type safe layer on the RPC architectural design. Traditionally, we use the REST API. It has <a target="_blank" href="https://restfulapi.net/http-methods/">GET, POST, PULL, and other request types</a>. In tRPC, there are no request types.</p>
<p>Every request to the tRPC back end passes through a query system and gets a response from the tRPC back end based on the input and query.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-10.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://adeesh.hashnode.dev/building-an-express-trpc-and-react-monorepo-setup-with-yarn-workspaces-tailwind-zod-and-react-query"><em>Adeesh Sharma</em></a></p>
<p>And instead, built-in functions are available with tRPC and react-query that will process your requests. Every request gets treated the same. It depends on whether the API endpoint accepts an input, throws an output, mutates it, and so on.</p>
<p>When using REST, you create a main folder named <code>/api</code> and route files inside them. For tRPC, you don't need any folder with many files. You need a few built-in functions and a simplified react-query system.</p>
<p>You don't need to use <code>fetch()</code>, process the output, and so on. tRPC operates using URLs representing a specific query, as you'll see shortly.</p>
<h3 id="heading-why-do-we-need-trpc">Why do we need tRPC?</h3>
<p>tRPC makes RPC type safe. It means your client cannot send data that the server cannot take. I cannot pass a string for a number-based property.</p>
<p>If the client tries to do this, you'll immediately receive an error — <strong>Invalid Type</strong>. The IDE and browsers throw errors if the datatypes mismatch.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/type-error.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>tRPC Type safe Error after submitting unexpected datatype value</em></p>
<p>Type safety is crucial for applications using JavaScript. So tRPC leverages <a target="_blank" href="https://www.typescriptlang.org/">TypeScript</a>. This makes it easier to create routes and perform operations on the back end.</p>
<p>tRPC requires a library named <a target="_blank" href="https://zod.dev/">Zod</a>. It helps tRPC to build the data schema of each route. A schema is an object with properties and an equivalent datatype linked to each property.</p>
<p>For example, if an API route requires the user's details, you would create an object on the back end and assign a datatype to each property using Zod.</p>
<p>On the front end, tRPC will verify whether the data provided by the user or API request matches the datatypes registered by the back end. tRPC leverages this type safe integration between the front end and back end.</p>
<p>Let's see how tRPC, Zod, and other libraries work together in a demonstration project.</p>
<h2 id="heading-how-to-use-trpc">How to Use tRPC</h2>
<p>You can spin up an Express server in a few seconds and start writing tRPC routes and queries — it's easy.</p>
<p>Traditionally, the client-side (front-end) and server-side (back-end) are separated. We will follow that separation for this example.</p>
<p>Let's start by creating the client side using React and the server side using Express + CORS to connect them.</p>
<h3 id="heading-folder-structure">Folder Structure</h3>
<p>First, create a directory named <code>tRPC Demo</code>. Inside this directory, create another directory named <code>trpclibrary</code> to separate the client and server sides and execute them together as a library later.</p>
<p>Inside the <code>trpclibrary</code> directory, you'll place your server (Express) and client (React) soon.</p>
<p>Inside the <code>tRPC Demo</code> root directory, insert a <code>package.json</code> file with the following code to interconnect all the folders and run the client and server-side with a single command.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"trpclibrary"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>,
  <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"start"</span>: <span class="hljs-string">"concurrently \"wsrun --parallel start\""</span>
  },
  <span class="hljs-attr">"workspaces"</span>: [
    <span class="hljs-string">"trpclibrary/*"</span>
  ],
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"concurrently"</span>: <span class="hljs-string">"^5.2.0"</span>,
    <span class="hljs-attr">"wsrun"</span>: <span class="hljs-string">"^5.2.0"</span>
  }
}
</code></pre>
<p>After setting up the <code>package.json</code> in the root directory, you'll start by setting up your Express server inside the <code>trpclibrary</code> directory.</p>
<p>Tip: Use the <code>cd &lt;folder_name&gt;</code> command to enter into a folder using the terminal and execute commands. In our case, you are in the root directory. So <code>cd .\trpclibrary</code> will help you. You can use the VS Code terminal, too.</p>
<p>You will use the <code>npx create-mf-app</code> starter command to initiate your server with a pre-defined template that saves you some time.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/sever-side-x2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Server-Side Setup</em></p>
<p>You may get errors stating that you don't have Express or other libraries installed. Don't worry – you'll install every required library shortly.</p>
<p>After creating the server, let's make the client using React and the same command in the same <code>trpclibrary</code> directory.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-11.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Client-Side Setup</em></p>
<p>That's your React client-side ready. But you might be overwhelmed with all the errors concerning modules and packages. So, let's download them first.</p>
<p>I'm using yarn, and I recommend you do the same. Use the <code>yarn</code> command in the root <code>trpcDemo</code> directory.</p>
<p>Tip: You can use the <code>cd ..</code> command to escape the current directory and enter the outer one.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-13.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Your server-side, client-side, or both might not have the TS Configuration file. So I recommend installing it using the <code>npx tsc --init</code> command within both directories.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-14.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>TS Configuration File Initialization</em></p>
<p>Now, you need to download tRPC, CORS, and Zod to your server side of the project.</p>
<p>As of 2nd July 2024, the <a target="_blank" href="https://www.npmjs.com/package/@trpc/server"><em>@trpc/server</em></a> package is at the latest version 10.45.2. Remember, even the <a target="_blank" href="https://www.npmjs.com/package/@trpc/client">client-side tRPC package</a> should be 10.45.2.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-16.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Installing Zod, CORS, and @trpc/server to the Server-Side</em></p>
<p>Then, you need to install <a target="_blank" href="https://www.npmjs.com/package/@trpc/client">@trpc/client</a>, <a target="_blank" href="https://www.npmjs.com/package/@trpc/react-query">@trpc/react-query</a>, <a target="_blank" href="https://www.npmjs.com/package/@tanstack/react-query">@tanstack/react-query</a>, <a target="_blank" href="https://www.npmjs.com/package/@trpc/server">@trpc/server</a>, and <a target="_blank" href="https://www.npmjs.com/package/zod">Zod</a> for the client side. You'll use the same "<a target="_blank" href="https://classic.yarnpkg.com/lang/en/docs/cli/add/">yarn add &lt;package_names&gt;</a>" command.</p>
<p>This time, I won't share the screenshot. Refer to the previous steps and try downloading them.</p>
<p>We've completed most installations and setup. Here's what your folder structure should look like:</p>
<pre><code class="lang-markdown">tRPC Demo
├── trpclibrary
│   ├── client-side (React App Folder)
│   ├── server-side (Express Server Folder)
└── package.json
</code></pre>
<h3 id="heading-trpc-setup">tRPC Setup</h3>
<p>Here's what we will do in this section:</p>
<ol>
<li><p>Create a tRPC instance with Context</p>
</li>
<li><p>Create tRPC routes and set up queries</p>
</li>
<li><p>Set up a base URL</p>
</li>
<li><p>Set up CORS</p>
</li>
</ol>
<p>Let's begin by creating a tRPC instance in the <code>index.ts</code> file of the server-side directory. As per the documentation, you should only initiate one instance per application.</p>
<p>Using the tRPC instance, create a router. A router helps you register routes where API requests arrive and are processed.</p>
<p>Routes are where you'll deal with the request and issue a response. A route is an API endpoint connected to a Base URL.</p>
<p>For example, <a target="_blank" href="http://localhost:3005/api/hello"><code>http://localhost:3005/api/hello</code></a> depicts an API endpoint named <code>hello</code> and the Base URL <code>api</code> to call the API endpoint.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { initTRPC } <span class="hljs-keyword">from</span> <span class="hljs-string">"@trpc/server"</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> trpcExpress <span class="hljs-keyword">from</span> <span class="hljs-string">"@trpc/server/adapters/express"</span>;

<span class="hljs-keyword">const</span> createContext = <span class="hljs-function">(<span class="hljs-params">{}: trpcExpress.CreateExpressContextOptions</span>) =&gt;</span> ({});
<span class="hljs-keyword">type</span> Context = Awaited&lt;ReturnType&lt;<span class="hljs-keyword">typeof</span> createContext&gt;&gt;;

<span class="hljs-keyword">const</span> trpc = initTRPC.context&lt;Context&gt;().create();
</code></pre>
<p>You must place this code above the existing boilerplate code in the <code>index.ts</code> file and below the import statements. It should be above the app and port variable declarations and below the express import statement.</p>
<p>Voilà! I created a tRPC instance using the <code>initTRPC</code> Builder from the <code>@trpc/server</code> package. We'll use this instance for everything related to the back end.</p>
<p>Additionally, I've added a <a target="_blank" href="https://trpc.io/docs/v10/server/context">Context</a> to the tRPC Router. It's a feature by tRPC. It allows you to put details like database connections and authentication information.</p>
<p>tRPC shares the Context between all tRPC procedures. It's an information and storage place to avoid code duplication and keep the code organized.</p>
<p>Up until now, you've initialized the tRPC instance with Context. Now, you'll code the router — so place the following code below the previous:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> zod <span class="hljs-keyword">from</span> <span class="hljs-string">"zod"</span>;

<span class="hljs-keyword">const</span> appRouter = trpc.router({
  hello: trpc.procedure
    .input(
      zod.object({
        name: zod.string(),
      })
    )
    .query(<span class="hljs-function">(<span class="hljs-params">{ input }</span>) =&gt;</span> {
      <span class="hljs-keyword">return</span> {
        name: input.name,
      };
    }),
});

<span class="hljs-keyword">export</span> <span class="hljs-keyword">type</span> AppRouter = <span class="hljs-keyword">typeof</span> appRouter;
</code></pre>
<p>Finally, you imported Zod. You also created an API endpoint named <code>hello</code> that takes input using the <code>input()</code> method and matches the API request from the user with the Zod object specified in this endpoint.</p>
<p>With this code, Zod and tRPC expect the front end to provide an object with a single string-based property named <code>name</code>. tRPC will use this property and respond using the input.</p>
<p>You took the input, destructured it, and processed it inside the <code>query()</code> method. All these methods are tRPC procedures. tRPC shares the Context among these procedures.</p>
<p>As I mentioned earlier, you will require the tRPC instance everywhere. I used it to create a router to store routes (API endpoints), register, and process them.</p>
<p>You can create unlimited routes inside the <code>router()</code> procedure. It is like a route handler. This endpoint is an object with each route acting as a property.</p>
<p>You'll require the procedure builder to access procedures like <code>query()</code>, <code>input()</code>, and so on. So we've bound it with the tRPC instance and accessed those methods.</p>
<p>Now, it's time to set the base URL. You will use the Express adapter from the <code>@trpc/server</code> library to set the base URL.</p>
<p>Place the following code above the <code>app.get()</code> routes inside the <code>index.ts</code> file:</p>
<pre><code class="lang-typescript">app.use(
  <span class="hljs-string">"/api"</span>,
  trpcExpress.createExpressMiddleware({
    router: appRouter,
    createContext,
  })
);
</code></pre>
<p><code>/api</code> represents your base URL. Every route will be on top of the <code>/api</code> URL. Now, your <code>hello</code> API endpoint has become <a target="_blank" href="http://localhost:3005/api/hello"><code>http://localhost:3005/api/hello</code></a>.</p>
<p>Let's try to test this using your browser. Do you remember that I asked you to create a <code>package.json</code> file with a pre-written code in the root tRPC Demo directory?</p>
<p>It was to run both the server and client-side as a library. Set your terminal to the root directory. Then, execute <code>yarn start</code> to run the server and client-side together and head to the <a target="_blank" href="http://localhost:3005/api/hello"><code>http://localhost:3005/api/hello</code></a> URL.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-23.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>tRPC Invalid Type Error</em></p>
<p>Uh-Oh! Did you receive an error? If the error says "Invalid Type", you're on the right path. See, this is where tRPC helps us.</p>
<p>In the above code, when I sent an API request as a user to the <code>hello</code> API endpoint, I didn't pass any object or values that tRPC expected for that endpoint.</p>
<p>tRPC expected an object with a string-based property named <code>name</code> with a value. When I didn't provide that, tRPC restricted my access. That's where it shines.</p>
<p>"That's all good, but what now?" You must connect the front end with the server side to send the object with the expected data.</p>
<p>There's one more thing left for the server side. CORS! It's simple to set it up. Find the Express Initialisation code in the <code>index.ts</code> file. It came with the Express template. Then, insert the following line:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> cors <span class="hljs-keyword">from</span> <span class="hljs-string">"cors"</span>;

app.use(cors());
</code></pre>
<p>Here's a hint: Search the port and app variable declaration in your <code>index.ts</code> file.</p>
<p>Once you insert the line, it might give you an error because you haven't installed the types of CORS yet. Go to your terminal and install <code>@types/cors</code> inside the server-side directory.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-24.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>@types/cors download.</em></p>
<p>CORS is ready and secure. Your server side is ready! Now, let's try to connect the server side with the client side using the respective libraries.</p>
<p>Before we shift to the client side, I want to make sure that we're on the same page. So far, you've coded an instance of tRPC, formed a Router, set a base URL, and tested the API endpoints with optional Context.</p>
<p>You coded all this inside the server-side <code>index.ts</code> file. Let's move to the client side and conquer the last part of this tutorial.</p>
<h3 id="heading-client-side">Client-Side</h3>
<p>We've already downloaded the required packages. We'll start by creating a <code>trpc.ts</code> file in the <code>/src</code> directory of the client-side directory. It will handle the queries and requests issued by the front end.</p>
<p>You created a tRPC instance to build the router and other components on the server side, right? Well, now you have to do the same on the client side. You need to create a client-side tRPC instance using <code>@trpc/react-query</code>.</p>
<p>Also, since you want to link your client-side tRPC instance with the server-side one, you have to import the server-side tRPC instance and its type.</p>
<p>To import the server-side tRPC instance, insert a <code>main</code> property inside the <code>package.json</code> file on the server side. It sets the <code>index.ts</code> as the entry file when you import the server-side folder on your client side.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-25.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Server-Side</em> <code>package.json</code> file.</p>
<p>After setting that property, you can import the tRPC instance into the client side using the terminal. For me, the back end is called <code>server-side</code> in my <code>package.json</code> file inside the server-side directory with version <code>1.0.0</code>.</p>
<p>So I'll execute <code>yarn add server-side@1.0.0</code> inside the client-side terminal. The installation might look familiar because this is how developers build libraries.</p>
<p>This command should add your server-side folder as a package in the client-side node modules directory. You can verify that with the <code>package.json</code> file on the client side.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-26.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Client-Side</em> <code>package.json</code> file.</p>
<p>It should contain your server-side package name as a dependency.</p>
<p>In other words, you installed the server-side package in your client-side application. You can now import the server-side tRPC and use it like a library.</p>
<p>If you remember earlier, we added an extra export AppRouter line while creating the router on the server side. We did that because we had to import the AppRouter type on the client side to use the server-side tRPC instance on the client side.</p>
<p>Here's how the <code>trpc.ts</code> file should look like now:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> { createTRPCReact } <span class="hljs-keyword">from</span> <span class="hljs-string">"@trpc/react-query"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-keyword">type</span> { AppRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">"server-side"</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> trpc = createTRPCReact&lt;AppRouter&gt;();
</code></pre>
<p>With this code, you've created a client-side tRPC instance using the characteristics of the server-side tRPC instance.</p>
<p>Perfect. Now, let's create another file named <code>AppComponent.tsx</code> in the <code>/src</code> directory.</p>
<p>This file will hold your main App component. It will import the <code>trpc</code> client instance from the <code>trpc.ts</code> file and use it to call your <code>hello</code> API endpoint.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { trpc } <span class="hljs-keyword">from</span> <span class="hljs-string">"./trpc"</span>;
</code></pre>
<p>Since you created a client-side tRPC instance, you can access all API endpoints on the client side and call the <code>useQuery()</code> method to send requests to those API endpoints.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { trpc } <span class="hljs-keyword">from</span> <span class="hljs-string">"./trpc"</span>;

<span class="hljs-keyword">const</span> AppComponent = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> userQuery = trpc.hello.useQuery({ name: <span class="hljs-string">"Afan"</span> });

  <span class="hljs-keyword">return</span> (
    &lt;div className=<span class="hljs-string">"mt-10 text-3xl mx-auto max-w-6xl"</span>&gt;
      &lt;div&gt;{<span class="hljs-built_in">JSON</span>.stringify(userQuery.data?.name)}&lt;/div&gt;
    &lt;/div&gt;
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> AppComponent;
</code></pre>
<p>If you remember, the <code>hello</code> API endpoint requires an object with the string-based <code>name</code> property with a value. So you'll pass the object using the <code>useQuery()</code> method with the value to avoid tRPC mismatches.</p>
<p>Inside the JSX code, you'll destructure the API response sent by the API endpoint using the <code>JSON.stringify()</code> method and access the result by the API endpoint.</p>
<p>Your <code>AppComponent.tsx</code> file is a standard React component. So you need to import this component into the main <code>App.tsx</code> file. <code>App.tsx</code> on the client side is equivalent to <code>index.ts</code> on the server side.</p>
<p>For the <code>App.tsx</code> file, you will follow a similar setup. Import the client tRPC instance from the <code>trpc.ts</code> file. Then, set the base URL and set up React Query.</p>
<p>You'll import React Query from TanStack, trpc from the <code>./trpc.ts</code> file, <code>httpBatchLink</code> from <code>@trpc/client</code>, <code>useState</code> from React, and your <code>AppComponent</code> from the <code>AppComponent.tsx</code> file.</p>
<pre><code class="lang-typescript"><span class="hljs-comment">// Default Import Statements</span>

<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom/client"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.scss"</span>;

<span class="hljs-comment">// Add the following Import Statements</span>

<span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">import</span> { trpc } <span class="hljs-keyword">from</span> <span class="hljs-string">"./trpc"</span>;
<span class="hljs-keyword">import</span> { httpBatchLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"@trpc/client"</span>;
<span class="hljs-keyword">import</span> AppComponent <span class="hljs-keyword">from</span> <span class="hljs-string">"./AppComponent"</span>;
<span class="hljs-keyword">import</span> { QueryClient, QueryClientProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">"@tanstack/react-query"</span>;
</code></pre>
<p>You will use each import statement. Ignore any error about imported objects not being used. Then, create a React Query client instance as you did for tRPC.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> QueryClient();
</code></pre>
<p>Once you've done that, you need to set up the Base URL. You'll do that inside the primary App function.</p>
<p>Also, shift the following code statements from below the App function to the top of the App function beneath the React Query client declaration statement.</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"app"</span>);
<span class="hljs-keyword">if</span> (!rootElement) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to find the root element"</span>);

<span class="hljs-keyword">const</span> root = ReactDOM.createRoot(rootElement <span class="hljs-keyword">as</span> HTMLElement);
</code></pre>
<p>Then, remove the default JSX HTML code from the App function. You can safely delete all HTML code in the App function.</p>
<p>Next, you need to set up the Base URL for the client side. Whenever the front-end invokes the API endpoints, it will use this Base URL. It should match the Base URL you set on the server side.</p>
<p>Replace your App function code from HTML to the following Base URL code:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [trpcClient] = useState(<span class="hljs-function">() =&gt;</span>
    trpc.createClient({
      links: [
        httpBatchLink({
          <span class="hljs-comment">// Base URL</span>
          url: <span class="hljs-string">"http://localhost:3005/api"</span>,
        }),
      ],
    })
  );

  <span class="hljs-keyword">return</span> &lt;&gt;&lt;/&gt;;
};
</code></pre>
<p>Your <code>App.tsx</code> file should look like this:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">"react-dom/client"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"./index.scss"</span>;

<span class="hljs-keyword">import</span> { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-keyword">import</span> { trpc } <span class="hljs-keyword">from</span> <span class="hljs-string">"./trpc"</span>;
<span class="hljs-keyword">import</span> { httpBatchLink } <span class="hljs-keyword">from</span> <span class="hljs-string">"@trpc/client"</span>;
<span class="hljs-keyword">import</span> AppComponent <span class="hljs-keyword">from</span> <span class="hljs-string">"./AppComponent"</span>;
<span class="hljs-keyword">import</span> { QueryClient, QueryClientProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">"@tanstack/react-query"</span>;

<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> QueryClient();

<span class="hljs-keyword">const</span> rootElement = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"app"</span>);
<span class="hljs-keyword">if</span> (!rootElement) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Failed to find the root element"</span>);

<span class="hljs-keyword">const</span> root = ReactDOM.createRoot(rootElement <span class="hljs-keyword">as</span> HTMLElement);

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [trpcClient] = useState(<span class="hljs-function">() =&gt;</span>
    trpc.createClient({
      links: [
        httpBatchLink({
          <span class="hljs-comment">// Base URL</span>
          url: <span class="hljs-string">"http://localhost:3005/api"</span>,
        }),
      ],
    })
  );

  <span class="hljs-keyword">return</span> &lt;&gt;&lt;/&gt;;
};

root.render(&lt;App /&gt;);
</code></pre>
<p>I haven't talked much about the <code>return</code> statement yet. So, let's do that now. We won't keep the <code>return</code> statement empty.</p>
<p>It will display the data returned by the API endpoint, which should be the string you submitted using the <code>useQuery()</code> method in the <code>AppComponent.tsx</code> component file.</p>
<p>The <code>return</code> statement is for the wrappers and the AppComponent component. If you require your components and pages to use tRPC, React Query, and so on, you must wrap your components like <code>AppComponent</code> with <code>Providers</code> of these libraries.</p>
<pre><code class="lang-typescript">  <span class="hljs-keyword">return</span> (
    <span class="hljs-comment">// tRPC Provider</span>
    &lt;trpc.Provider client={trpcClient} queryClient={client}&gt;
      {<span class="hljs-comment">/* React Query Provider */</span>}
      &lt;QueryClientProvider client={client}&gt;
        {<span class="hljs-comment">/* HTML React Component */</span>}
        &lt;AppComponent /&gt;
      &lt;/QueryClientProvider&gt;
    &lt;/trpc.Provider&gt;
  );
</code></pre>
<p>Now, you'll wrap the <code>AppComponent</code> component with React Query and pass the React Query client instance you created using <code>QueryClient()</code> in this file. Then, you'll wrap the React Query Provider with the tRPC Provider.</p>
<p>The tRPC provider requires the React Query Client and the tRPC Client with the Base URL. So we will provide that information, too.</p>
<p>Once you pass the required information and match your code with mine, you can visit <a target="_blank" href="http://localhost:3000">http://localhost:3000</a> and look at the output. It will display the data you passed using the <code>hello</code> API endpoint.</p>
<p>Note: You should run the <code>yarn start</code> command in your root <code>tRPC Demo</code> directory to turn on the localhost ports to view the output.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/image-27.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Output Image</em></p>
<p>Voilà! We have it all ready. tRPC invokes the <code>hello</code> API endpoint from the front end. It prioritizes Type Safety and uses TypeScript to avoid millions of other JavaScript problems.</p>
<p>You can add more routes and API endpoints like <code>hello</code> in your Route handler. It's as easy as adding a new property to an object. That's how tRPC makes your life easier.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>tRPC is a type safe RPC-styled library. It integrates RPC with TypeScript to eliminate REST, <code>fetch()</code>, and other techniques to create and make API calls.</p>
<p>It acts as an alternative to REST and Fetch. I'll use it for the foreseeable future.</p>
<p>I've enjoyed learning about this newer technology. There may be some flaws in this article, but I'm a fellow learner, so report my mistakes whenever you can and help me improve.</p>
<p>Subscribe to <a target="_blank" href="https://www.freecodecamp.org/news/p/96029b5d-38ad-4b3c-a021-661b70eb6dd3/Subscribe%20to%20my%20newsletter%20for%20the%20weekly%20emails%20about%20Software%20Engineering,%20Tech%20Jobs%20&amp;%20Careers,%20and%20resources%20to%20excel%20in%20your%20career.">my newsletter</a> for the weekly emails about Software Engineering, Tech Jobs and Careers, and resources, including paid articles for free, to help you excel in your career.</p>
<p><em>Hope to see you in the next one ✌️</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Frameworks vs Laravel – Which Should You Choose for Web Development? ]]>
                </title>
                <description>
                    <![CDATA[ For a long time, developers in the JavaScript ecosystem have tried to launch Laravel-like frameworks for JavaScript. We've seen Blitz for NextJS, Adonis for NodeJS, RedwoodJS, and more. All these frameworks tried to build a Laravel for JavaScript. Bu... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-frameworks-vs-laravel/</link>
                <guid isPermaLink="false">66d45d5a33b83c4378a517b0</guid>
                
                    <category>
                        <![CDATA[ framework ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Laravel ]]>
                    </category>
                
                    <category>
                        <![CDATA[ PHP ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Afan Khan ]]>
                </dc:creator>
                <pubDate>Mon, 17 Jun 2024 14:28:11 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/06/Group-3148.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>For a long time, developers in the JavaScript ecosystem have tried to launch Laravel-like frameworks for JavaScript.</p>
<p>We've seen Blitz for NextJS, Adonis for NodeJS, RedwoodJS, and more. All these frameworks tried to build a Laravel for JavaScript.</p>
<p>But none of them were really successful. Why? We'll figure it out in this article. It's worth going over because beginners learning JavaScript struggle to find an all-in-one solution.</p>
<p>This means it can be tough to choose a framework (or library) in JavaScript when you have so many choices – like React, Vue, Angular, Svelte, NextJS, Meteor, and more.</p>
<p>Meanwhile, PHP offers one simple solution: Laravel. It's a one-stop shop with many frameworks and libraries combined into one. But is it the right option for you?</p>
<p>In this article, I'll dive deeper into why the JavaScript ecosystem doesn't have a Laravel-like framework and whether you should switch to PHP for a one-stop solution like Laravel.</p>
<p>I will discuss the advantages and disadvantages of using JavaScript frameworks compared to a one-stop solution like Laravel and end it with a comparison table to help you decide.</p>
<h2 id="heading-what-well-cover-in-this-article">What we'll cover in this article:</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-why-javascript-vs-laravel">Why JavaScript vs Laravel</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-javascript-lacks-an-all-in-one-solution">Why JavaScript lacks an all-in-one solution</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-javascript-frameworks-and-libraries">Benefits of JavaScript frameworks and libraries</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-laravel">Benefits of Laravel</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-differences-between-laravel-and-js-tools">Differences between Laravel and JS tools</a></p>
</li>
</ol>
<h2 id="heading-why-javascript-vs-laravel">Why JavaScript vs Laravel</h2>
<p>When beginners decide to learn Web Development, they often choose between JavaScript/TypeScript and PHP. Other solutions require more expertise, so most beginners gravitate towards these two languages.</p>
<p>Many JavaScript beginners eventually find its vast ecosystem overwhelming, whereas PHP learners often feel constrained by the limited frameworks and lack of advanced capabilities.</p>
<p>To deal with that confusion, I am comparing the tools available for JavaScript and PHP, not the languages directly. Since PHP has an all-in-one solution most developers use, I'll use Laravel for the comparison.</p>
<p>JavaScript lacks a single all-in-one solution or a specific representative framework. Therefore, I'll refer to tools like React, Vue, Angular, and so on as "JavaScript Frameworks."</p>
<p>Let's begin by understanding why the JavaScript ecosystem lacks a Laravel-like framework.</p>
<h2 id="heading-why-javascript-lacks-an-all-in-one-solution">Why JavaScript Lacks an All-in-One Solution</h2>
<p>I'll begin by acknowledging that JavaScript developers have tried in the past to make a Laravel for JavaScript.</p>
<p>Start with RedwoodJS. It's an open-source full-stack JavaScript framework started by Tom Preston-Werner, the co-founder and former CEO of GitHub. It helps developers build their applications and ship faster.</p>
<p>Redwood has many innovative solutions packed into one framework. Sounds familiar? That's precisely like Laravel. Do you see anyone shipping using Redwood? Most likely not.</p>
<p>The next option is Blitz. It makes the same promise as Redwood: a replacement for NextJS. Blitz is an all-in-one solution with custom built-in solutions for authentication, database, and so on.</p>
<p>But Blitz doesn't allow developers to choose what they want to use, like Laravel. It has custom solutions crafted by the core team. Developers cannot replace any solution piece with some other recommended solution.</p>
<p>Customization is a point worth noting in the JavaScript ecosystem, and I'll explain why as we uncover more frameworks. But Blitz didn't succeed either.</p>
<p>Then, there's AdonisJS. But, I think you get the point. Each all-in-one solution in JavaScript has tried to implement their version of Laravel – and none of them really took hold.</p>
<p>Blitz, Adonis, and Redwood followed a theme: a full-stack framework of ready-made non-customizable solutions for JavaScript developers to build applications.</p>
<p>These frameworks didn't gain popularity because they didn't meet the needs of the developers they aimed to serve.</p>
<p>The JavaScript ecosystem is unique, offering many solutions for the same problem. There's a reason for this diversity, and why those frameworks didn't succeed.</p>
<p>Let's discuss the reasons and benefits of the current popular JavaScript tools, as they directly impact the choice between JavaScript frameworks and Laravel. As I outline each benefit, consider whether it aligns with your needs.</p>
<h2 id="heading-benefits-of-javascript-frameworks-and-libraries">Benefits of JavaScript Frameworks and Libraries</h2>
<p>JavaScript frameworks and libraries are collections of pre-written resources, techniques, and functions created by developers to save time and effort.</p>
<p>These frameworks began emerging in the 2000s, with notable ones like React, Svelte, Angular, NextJS, and Vue standing out over time.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/image-80.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://raygun.com/blog/popular-javascript-frameworks/"><em>Raygun</em></a></p>
<p>However, having more options doesn't necessarily mean greater effectiveness. Below, we’ll explore the benefits of using JavaScript frameworks and why all-in-one solutions haven't succeeded in the JavaScript ecosystem.</p>
<h3 id="heading-versatility">Versatility</h3>
<p>It's easier to switch or choose one solution over the other in the JavaScript ecosystem. For example, to implement authentication, we have Clerk, Auth0, AuthJS, NextAuth, Supabase, and more.</p>
<p>The JavaScript ecosystem always had multiple options for a specific solution and startups dedicated to providing bespoke solutions. If you didn't like Clerk, you could use NextAuth for more customization.</p>
<p>JavaScript frameworks are highly versatile. When there are ten solutions for one problem, developers can easily switch and see what works for them.</p>
<p>Blitz and other frameworks couldn't succeed for this reason. If a developer disliked Blitz's approach to authentication, they would just switch to another solution.</p>
<h3 id="heading-npm">NPM</h3>
<p>Unlike JavaScript, other languages don't have an ecosystem around building tools for others, like libraries or packages. It's not as easy in those languages.</p>
<p>The JavaScript ecosystem has an entire registry filled with packages and libraries for the smallest solutions, like emails. JavaScript has many packages, frameworks, and libraries for each solution.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/image-81.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source:</em> <a target="_blank" href="https://kinsta.com/knowledgebase/what-is-npm/"><em>Kinsta</em></a></p>
<p>Ironically, even the NPM registry has competitors. There are multiple other registries with unique packages and features. JavaScript follows the versatility rule everywhere, but does that indicate reliability? Maybe not.</p>
<h3 id="heading-startups">Startups</h3>
<p>Each solution for a specific problem is likely a startup funded by VC firms and Angel Investors.</p>
<p>JavaScript-based startups have the capital and the resources to conduct in-depth research and hire the best talent to build bespoke solutions for a targeted problem.</p>
<p>With unlimited resources allocated to a specific topic or problem, the solutions are specific and cater to different needs to their developers.</p>
<p>In return, JavaScript developers pay these startups for those solutions and startups are incentivised to create better solutions for the ecosystem.</p>
<h3 id="heading-the-common-dev">The Common Dev</h3>
<p>Any developer in the JavaScript ecosystem can identify a problem during their experience using a specific technology, build a solution, and offer it as a product.</p>
<p>JavaScript developers have the liberty to build and sell their solutions because startups push the idea of paying for bespoke solutions and developers have no problem doing that.</p>
<p>Take <a target="_blank" href="https://x.com/marc_louvion">Marc Lou</a> as an example. He built <a target="_blank" href="https://shipfa.st/">ShipFast</a> and other products that churn over $100,000 per month. He is an example of the possibilities in the field.</p>
<p>But perhaps these benefits and reasons seem intangible to you. Don’t worry—the web development domain offers more than one alternative. Let’s explore Laravel.</p>
<h2 id="heading-laravel">Laravel</h2>
<p>Laravel is a framework for the PHP programming language. Taylor Otwell introduced Laravel in 2011.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/image-45.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source: Cloudways</em></p>
<p>Laravel offers many solutions packed into one tool, and can be a one-stop solution for everything. It offers authentication, debugging, serverless deployment, user-friendly form systems, starter kits, and so on.</p>
<p>The solutions provided by Laravel are many, but without the core language, they don't mean anything. Laravel is all about PHP.</p>
<p>PHP is a general-purpose scripting language. Rasmus Lardof introduced it to developers in the 1990s. Since then, the language has evolved with significant updates in 2004, 2015, and 2020.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/image-46.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Source: Gecko Dynamics</em></p>
<p>Before JavaScript frameworks had taken off, developers used PHP with JavaScript.</p>
<p>Laravel is a back-end framework and PHP focuses on the server-side. JavaScript without a framework is a client-side language at its core.</p>
<p>Developers traditionally used JavaScript on the client-side and PHP on the server-side to create full-fledged applications.</p>
<p>However, the tech stack landscape shifted when Laravel gained popularity and frameworks like React began to dominate the JavaScript ecosystem.</p>
<p>Many developers worldwide, especially from countries like India, are doubling down on PHP because of its robust and reliable nature.</p>
<p>Government websites and large infrastructures use Laravel (PHP) because it supports a variety of solutions in one place. Many developers don't want to combine and switch technologies.</p>
<p>Companies and individuals using PHP or Laravel want to ship quickly, make changes, and avoid learning a new library or technology at every step of the way like JavaScript.</p>
<p>PHP only has JavaScript as its direct rival that challenges its existence. However, JavaScript isn't the best for many situations. Let's see how Laravel performs better.</p>
<h2 id="heading-benefits-of-laravel">Benefits of Laravel</h2>
<h3 id="heading-one-stop-solution">One-Stop Solution</h3>
<p>Laravel is an all-in-one framework. It contains everything a developer would reasonably require to build compact, reliable, and robust solutions.</p>
<p>You don't need to spend time searching for various libraries and frameworks that may or may not integrate well with your foundational technology—it's all built-in for you.</p>
<p>If speed of deployment is your priority, Laravel is the ideal choice.</p>
<p>Moreover, for PHP developers using Laravel, there's less need to learn new libraries, syntax, and more because everything is integrated seamlessly.</p>
<h3 id="heading-less-wasted-time">Less Wasted Time</h3>
<p>The PHP ecosystem has fewer solutions for specific problems. It's difficult to identify a specific solution for a specific problem.</p>
<p>Developers rather choose an all-in-one solution than invest time in finding bespoke solutions for specific problems.</p>
<p>That's where the PHP ecosystem shines because developers don't spend a lot of time trying out different options. They already have everything in one place.</p>
<p>If there's a new project, their go-to choice is Laravel. In the JavaScript land, if a developer isn't using an existing solution or tech stack, they are likely finding a new one or creating one.</p>
<h3 id="heading-stable-and-robust">Stable and Robust</h3>
<p>Web Applications created using PHP are robust because the framework is battle-tested. You can create anything using it.</p>
<p>You could sabotage the entire application if you mess up with one choice of library or framework in a JavaScript project. But it's not the same with Laravel.</p>
<p>Since Laravel has fewer solutions, the chances of sabotaging are reduced to the bare minimum.</p>
<h3 id="heading-no-catching-up">No Catching Up</h3>
<p>With Laravel, you don't need to catch up with new technologies and frameworks frequently. You can learn one framework, library, or skill and consider it useful for a long time.</p>
<p>It's the opposite in JavaScript. Unlike JS, Laravel has more stable and less frequent updates.</p>
<p>The PHP ecosystem receives fewer updates so developers can reliably use the existing features without worrying about another framework or library providing a better solution or another version.</p>
<p>A new framework or library doesn't pop up each week. You don't need to follow up on versions frequently.</p>
<h3 id="heading-a-massive-community">A Massive Community</h3>
<p>Laravel was first launched a decade ago. It has gained popularity over time and invited many developers to create communities and share ideas.</p>
<p>Every PHP developer uses Laravel. If you ever need any help, you always have developers helping you.</p>
<h3 id="heading-free-solutions">Free Solutions</h3>
<p>Most PHP frameworks and libraries are open-source and free to access. Startups are rare in the PHP ecosystem.</p>
<p>If a developer is offering any bespoke solutions, most are public, especially free.</p>
<p>Any PHP developer can refer to those open-source free solutions, contribute to them, and extend those solutions further.</p>
<h2 id="heading-differences-between-laravel-and-js-tools">Differences Between Laravel and JS Tools</h2>
<p>You can create any type of application using both of these technologies. But the experience won't be exactly the same, of course. Here are some of the differences:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Features</td><td>JavaScript Frameworks</td><td>Laravel</td></tr>
</thead>
<tbody>
<tr>
<td>Beginner Friendly</td><td>They often use TypeScript, which is beginner-friendly and beneficial for developers switching from C, C#, Java, and C++</td><td>Laravel is easier to adopt if you have a grasp of PHP or other JavaScript frameworks.</td></tr>
<tr>
<td>Flexibility</td><td>High flexibility with the ability to choose different tools and libraries.</td><td>Less flexibility but provides a cohesive, opinionated structure.</td></tr>
<tr>
<td>Salary</td><td>Developers using JavaScript frameworks have higher salaries.</td><td>Laravel developers get a generally lower salary than JavaScript/TypeScript developers.</td></tr>
<tr>
<td>Availability of tools</td><td>JavaScript frameworks are widely available and ready-to-use. You can download them from the NPM registry and start immediately.</td><td>Laravel offers solutions for a wide range of problems, but if you encounter issues with their built-in solutions, you have fewer alternatives.</td></tr>
<tr>
<td>Security</td><td>Depends on the back-end setup of the respective framework, but there are options to choose from if the security measures lack.</td><td>It has built-in security features like CSRF and encryption.</td></tr>
<tr>
<td>Documentation</td><td>Depends on the framework. React, NextJS, and a few other frameworks have the most detailed documentations with code snippets and explanations.</td><td>Laravel has a structured and organised documentation with code snippets and explanations.</td></tr>
<tr>
<td>Testing &amp; Debugging</td><td>JavaScript frameworks have multiple testing libraries, like Jest.</td><td>Laravel has built-in tools, like PHPUnit intergration for testing.</td></tr>
<tr>
<td>Learning Curve</td><td>JavaScript frameworks have a higher learning curve due to frequent and significant updates, as well as the regular release of new frameworks.</td><td>Laravel has a gentle learning curve because its built-in solutions are easy to adopt, even for those familiar with JavaScript frameworks.</td></tr>
<tr>
<td>Scalability</td><td>JavaScript frameworks are highly scalable, allowing you to swap libraries as needed to maximize performance.</td><td>Laravel has limited options to modify and swap solutions. Hence, it is less scalable, but reliable.</td></tr>
<tr>
<td>Deployment &amp; Hosting</td><td>JavaScript frameworks can be deployed with any VPS, or using Vercel with a few clicks. However, it requires NodeJS on the server.</td><td>Laravel projects can be hosted on any server supporting PHP.</td></tr>
<tr>
<td>Customization</td><td>JavaScript frameworks allow for easy swapping and customization, enabling you to tailor libraries or frameworks to suit your needs.</td><td>Laravel solutions are limited and cannot be modified.</td></tr>
<tr>
<td>Performance</td><td>JavaScript frameworks have high-performance for dynamic, interactive Uls.</td><td>Laravel has high-performance for back-end processes and server-side rendering.</td></tr>
<tr>
<td>Future</td><td>JavaScript frameworks continue to grow, with more startups being founded and developers launching their own solutions.</td><td>Laravel continuously evolves, releasing new sub-frameworks and solutions to address problems in the most effective way possible.</td></tr>
</tbody>
</table>
</div><p>And that's it! I hope you enjoyed the article and learned something new. If you want, you can also follow me on <a target="_blank" href="https://x.com/whyafan">Twitter</a> (X) or <a target="_blank" href="https://www.linkedin.com/in/khanafan/">LinkedIn</a>. Also, I've built a <a target="_blank" href="https://store.afankhan.com/l/codenexus">Notion Dashboard</a> for Software Engineers to ace their coding interviews.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
