Learn Gatsby Β· 1/12

Introduction to Gatsby & Environment Setup

8 min read

Course: Gatsby

Topic: Introduction to Gatsby & Environment Setup

Welcome! πŸ‘‹ Let's dive into the world of Gatsby. Think of this guide as your friendly co-pilot as you begin your journey. We'll take it one step at a time, making sure you understand the "why" behind the "how". No boring lectures here, just the good stuff you need to get building.


1. What is Gatsby? The Jamstack Concept Explained

The Problem with Traditional Websites

Imagine a busy restaurant. In a traditional website model, every time a customer (a user visiting your site) places an order, the chef (the server) has to run to the back, gather all the raw ingredients (data from a database), cook the meal from scratch (build the webpage), and then serve it. This happens for every single customer. 🍳

It works, but it can be slow and inefficient, especially when you get a lot of customers at once. It also means the kitchen (your server) has to be a complex, powerful, and expensive setup.

The Solution: The Jamstack ⚑

Jamstack is not a specific technology, but a modern architectural approach to building websites. The "JAM" stands for:

  • JavaScript: To handle any dynamic programming during the request/response cycle.
  • APIs: Reusable APIs are used for all server-side actions or database operations.
  • Markup: Prebuilt Markup (like HTML) is served directly to the browser.

In our restaurant analogy, the Jamstack approach is like a high-end meal prep service. The chef prepares and cooks almost the entire meal before the restaurant even opens. All the meals (your web pages) are pre-built and stored, ready to be served instantly. When a customer orders, the waiter just grabs a pre-packaged, ready-to-go meal and serves it. It's incredibly fast, less work for the server at that moment, and much more secure because there's less back-and-forth to the kitchen.

So, What is Gatsby? πŸ€”

Gatsby is a React-based, open-source framework for building super-fast, optimized, and secure websites and apps. It's one of the most popular tools for building with the Jamstack architecture.

Gatsby is your master chef. During a process called "build time," Gatsby pulls in data from all your sources (like a CMS, Markdown files, APIs, etc.), uses React to build all the pages of your site, and spits out a folder of highly optimized static assets (HTML, CSS, JavaScript).

When a user visits your site, they get a pre-built HTML file, which loads almost instantly. Then, Gatsby magically hydrates the site into a full-blown React application, making it dynamic and interactive. It's the best of both worlds: the speed of a static site and the power of a dynamic app.

✨ Fun Fact: The name "Gatsby" is inspired by F. Scott Fitzgerald's novel, The Great Gatsby. The idea is that Gatsby.js allows you to build lavish, impressive websites with a well-structured and organized approach, much like the grand parties thrown by Jay Gatsby.


2. Setting Up Your Development Environment (Node.js & Git)

Before we can start building our magnificent Gatsby site, we need to make sure our workshop (your computer) has the right tools. You wouldn't build a table without a hammer and saw, right? For Gatsby, our essential tools are Node.js and Git.

What You'll Need

  1. Node.js & npm:

    • What is it? Node.js is a JavaScript runtime environment. In simple terms, it allows you to run JavaScript on your computer (on the "server-side"), not just inside a web browser. Gatsby's command-line tools and the entire build process are built using Node.js.
    • What is npm? When you install Node.js, you also get npm (Node Package Manager). Think of npm as a massive library of code packages. It helps you install and manage the dependencies (other tools and libraries) your project needs to run.
    • How to install:
      • The Gatsby documentation recommends using the latest LTS (Long-Term Support) version of Node.js.
      • The easiest way to install and manage Node versions is by using nvm (Node Version Manager).
      • First, install nvm by following the instructions for your operating system (see references).
      • Then, run these commands in your terminal:
        bash
        # Install the latest LTS version of Node.js
        nvm install --lts
        
        # Set the newly installed LTS version as the one to use
        nvm use --lts
      • To verify your installation, open a new terminal window and type node --version and npm --version. You should see version numbers appear!
  2. Git:

    • What is it? Git is a version control system. It's like a super-powered "undo" button for your entire project. It takes snapshots of your code, allowing you to track changes, revert to previous versions if you make a mistake, and collaborate with other developers without stepping on each other's toes.
    • Why do I need it? While you can technically run Gatsby without it, many starters and themes you'll use are managed through Git repositories. It's an industry-standard tool for all developers, so it's best to have it ready.
    • How to install:
      • Head over to the official Git website and download the installer for your operating system (see references).
      • Follow the installation steps. The default options are usually fine for beginners.
      • To verify, open your terminal and run git --version. You should see the installed Git version.

3. The Gatsby CLI: Bootstrapping Your First Hello World

Now that our environment is ready, let's get to the fun part! Manually creating all the files and folders for a new project is a real drag. Luckily, Gatsby provides a tool to do all the heavy lifting for us.

The Gatsby Command Line Interface (CLI) πŸ€–

The Gatsby CLI is a command-line tool that helps you create, manage, and develop Gatsby projects. It's your personal assistant for all things Gatsby.

  1. Install the Gatsby CLI:
    Open your terminal and run the following command. The -g flag installs it "globally" on your computer, so you can use the gatsby command from anywhere.

    bash
    npm install -g gatsby-cli
  2. Create Your First Gatsby Site:
    Let's create a new site. The gatsby new command will create a new folder, download a default starter template, and install all the necessary dependencies.

    bash
    # This will create a new directory called "my-first-gatsby-site"
    gatsby new my-first-gatsby-site

    This process might take a minute or two as it downloads everything. Go grab a coffee! β˜•

  3. Start the Development Server:
    Once it's done, navigate into your new project's directory and start the development server.

    bash
    # Move into the project folder
    cd my-first-gatsby-site
    
    # Start the development server
    gatsby develop

    Your terminal will show some output, and once it's ready, you'll see a message like this:

    livecodeserver
    You can now view my-first-gatsby-site in the browser.
    β €
    http://localhost:8000/

    Open your web browser and go to http://localhost:8000. VoilΓ ! You should see your brand new Gatsby site running live. πŸŽ‰

    Hot-Reloading Magic! ✨ Try opening the code for your new site in a code editor (like VS Code). Find the file at src/pages/index.js and change some of the text. As soon as you save the file, the browser will automatically refresh with your changes. This is called hot-reloading, and it's a huge time-saver during development.


4. Deconstructing the Gatsby Project Structure

Okay, so you've created a site, but what are all these files and folders that the Gatsby CLI just made? Let's take a look at the most important ones. Think of this as a blueprint of your new digital home.

nix
my-first-gatsby-site/
β”œβ”€β”€ .cache/
β”œβ”€β”€ node_modules/
β”œβ”€β”€ public/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ 404.js
β”‚   β”‚   └── index.js
β”‚   └── templates/
β”œβ”€β”€ .gitignore
β”œβ”€β”€ gatsby-browser.js
β”œβ”€β”€ gatsby-config.js
β”œβ”€β”€ gatsby-node.js
β”œβ”€β”€ gatsby-ssr.js
└── package.json

Don't be intimidated! You only need to focus on a few of these for now.

  • πŸ“ /src
    This is where you, the developer, will spend 99% of your time. It's the heart of your application.

    • πŸ“ /src/pages
      This is one of Gatsby's most magical folders. Any React component you create in this folder automatically becomes a page on your website. For example, src/pages/about.js will create a page at yoursite.com/about. The index.js file is your homepage. This is a core convention in Gatsby.
    • πŸ“ /src/components
      This folder is for reusable pieces of your user interface. Things like a header, footer, or a button that you want to use on multiple pages. This helps keep your code organized and DRY (Don't Repeat Yourself).
    • πŸ“ /src/images
      A convenient place to store your images. Gatsby has powerful image processing capabilities that you'll learn about later.
  • πŸ“„ gatsby-config.js
    This is the main configuration file for your Gatsby site. It's the recipe book. Here, you'll add plugins, site metadata (like the site title and description), and other customizations.

  • πŸ“„ package.json
    This file is the manifest of your project. It lists all of your project's dependencies (the npm packages it needs to run) and defines some helpful scripts (like gatsby develop). You rarely edit this file by hand; npm manages it for you when you add or remove packages.

The other files and folders (.cache, public, gatsby-node.js, etc.) are important, but you don't need to worry about them just yet. They are mostly handled by Gatsby itself. For now, focus on the /src directory and the gatsby-config.js file.


And that's it! You've taken your first steps into a larger world. You learned what the Jamstack is, how Gatsby fits in, set up your computer with the right tools, and created and explored your very first Gatsby project. Great job!

References

Challenge

You have been tasked with setting up the initial configuration and creating the first content page for a new Gatsby project.

Part 1: Configuration
Within the gatsby-config.js structure, define the siteMetadata for the website. The metadata object must include the following key-value pairs:

  • title: "Jamstack Powerhouse"
  • description: "A site built to explore the power of Gatsby."
  • author: "Alex Doe"

Part 2: Page Creation
Below the configuration code, create a new React functional component that represents the site's homepage. Assume this component will be saved as /src/pages/index.js.

  • The component must be named HomePage.
  • It must return a primary content element (<main>) that contains:
    • An <h1> with the text "Welcome to the Jamstack Powerhouse".
  • The component must be the default export.

Expected result

Your submission should be a single block of JavaScript code.

The first part must be a module.exports object containing a siteMetadata key. This key's value should be an object with three string properties: title, description, and author, matching the values provided in the instructions exactly.

The second part must be a complete React functional component named HomePage. It must import React at the top and use a default export. The component's JSX should return a <main> element containing a single <h1> with the exact text "Welcome to the Jamstack Powerhouse".

javascript

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.