Gatsby
A step-by-step guide on installing and configuring shadcn/ui components within your Gatsby project.
Update: We have added full support for React 19 and Tailwind v4 in the
canary release. See the docs for Tailwind v4 for more
information.
Step 1: Initialize Your Gatsby Project
Begin the installation process by creating a new Gatsby project using the create-gatsby command-line tool:
npm init gatsbyStep 2: Configure Your Gatsby Project with TypeScript and Tailwind CSS
During the project creation process, you will be prompted with a series of questions to configure your Gatsby project. Ensure you select TypeScript and Tailwind CSS when prompted:
✔ What would you like to call your site?
· your-app-name
✔ What would you like to name the folder where your site will be created?
· your-app-name
✔ Will you be using JavaScript or TypeScript?
· TypeScript
✔ Will you be using a CMS?
· Choose whatever you want
✔ Would you like to install a styling system?
· Tailwind CSS
✔ Would you like to install additional features with other plugins?
· Choose whatever you want
✔ Shall we do this? (Y/n) · YesStep 3: Configure Path Aliases in tsconfig.json
Modify your tsconfig.json file to include path aliases. This configuration is necessary for correctly resolving module paths within your project, particularly for components:
{
"compilerOptions": {
// ...
"baseUrl": ".",
"paths": {
"@/*": [
"./src/*"
]
}
// ...
}
}Step 4: Create or Modify gatsby-node.ts for Path Resolution
Create a gatsby-node.ts file at the root of your project if it doesn't already exist. Add the following code snippet to this file to configure Webpack for resolving the defined path aliases, ensuring your application can correctly locate modules:
export const onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
"@/components": path.resolve(__dirname, "src/components"),
"@/lib/utils": path.resolve(__dirname, "src/lib/utils"),
},
},
});
};Step 5: Execute the shadcn/ui Initialization Command
Run the shadcn/ui initialization command in your terminal to configure your project for using shadcn/ui components:
npx shadcn@latest initStep 6: Configure the components.json File
You will be guided through a series of questions to configure the components.json file. This file is essential for customizing the behavior and location of shadcn/ui components within your project. Respond to the prompts as follows:
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › ./src/styles/globals.css
Do you want to use CSS variables for colors? › no / yes
Where is your tailwind.config.js located? › tailwind.config.js
Configure the import alias for components: › @/components
Configure the import alias for utils: › @/lib/utils
Are you using React Server Components? › noStep 7: Begin Adding Components
You have successfully completed the initial setup. You can now begin adding individual shadcn/ui components to your Gatsby project as needed.
npx shadcn@latest add buttonExecuting the command shown above will add the Button component files to your project structure. You can then import and use the component in your Gatsby files as demonstrated below:
import { Button } from "@/components/ui/button";
export default function Home() {
return (
<div>
<Button>Click me</Button>
</div>
);
}