Skip to content
Eyup Ucmaz
TwitterGithubLinkedinMail Me

Build Your Own CRA Template

Build, React, Template2 min read

Computer monitor shows code editor.

Today we're gonna build our own create-react-app template. I have using a cra been a while. I clearing some files on app when i used cra. (Like test files, icons, and inside of app.js file). I thought why am i not using a template few days ago. Therefore i googled "how i create my own template?". After looking some blogs and documentation i built one. Let's take look how you can built your own.

Create a new App

Create a new react app as always you did. Basically;

1npx create-react-app cra-template-[template-name]

After all installation clear all files which are you don’t want to on your template. In this case my project is look like this 👇

folder structure

Adding files and dependencies

So add all dependencies usually you use on your projects. Just type on console npm i <dependency> or npm i <dependency> --save-dev install whatever you want. I installed my own also i deleted a few dependencies from package.json My dependencies is 👇

1"dependencies": {
2 "axios": "^0.25.0",
3 "bootstrap": "^5.1.3",
4 "react": "^17.0.2",
5 "react-bootstrap": "^2.1.2",
6 "react-dom": "^17.0.2",
7 "react-icons": "^4.3.1",
8 "react-router-dom": "6",
9 "react-scripts": "5.0.0",
10 "sass": "^1.49.7"
11}

I will also add components folder to my template. I added a dummy component in this folder.

Editing files

First I'll edit the index.js file. I imported the bootstrap.min.css file and BrowserRouter from react-router-dom your can also edit more but in my case those are all i need when started a new project. So tha my index file 👇

1import React from 'react';
2import ReactDOM from 'react-dom';
3import 'bootstrap/dist/css/bootstrap.min.css';
4import './index.scss';
5import { BrowserRouter } from 'react-router-dom';
6import App from './App';
7ReactDOM.render(
8 <BrowserRouter>
9 <App />
10 </BrowserRouter>,
11 document.getElementById('root')
12);

In App.js i removed the all jsx and added just h1 tag with lorem text also i edit the **README.md file. The README file also will shown in npm package web site.

Creating template folder and template.json

After all editing the project we're gonna build our actual template folder structure. In CRA documentation says the folder structure must have following structure,

1cra-template-[template-name]/
2 README.md (for npm)
3 template.json
4 package.json
5 template/
6 README.md (for projects created from this template)
7 gitignore
8 public/
9 index.html
10 src/
11 index.js (or index.tsx)

So, first all delete the node_modules folder. Then create template folder. Move the public and src folder to template folder. Also rename the .gitignore to gitignore then move to template folder. Last thing about template folder create a README.md file for projects created from this template.

Let’s create a template.json file and edit. Remember we already added our dependencies for our template. We’ll move the all of them to template.json file. My template.json file is;

1{
2 "package": {
3 "dependencies": {
4 "axios": "^0.25.0",
5 "bootstrap": "^5.1.3",
6 "react-bootstrap": "^2.1.2",
7 "react-icons": "^4.3.1",
8 "react-router-dom": "6",
9 "react-scripts": "5.0.0",
10 "sass": "^1.49.7"
11 },
12 "eslintConfig": {
13 "extends": ["react-app"]
14 }
15 }
16}

Copy all your from your package.json file. Thats all. 🙂

The last thing before publishing to npm we should edit the package.json file. I used the official template for edit my own. You can also use. So my package.json file is;

1{
2 "name": "cra-template-junior",
3 "version": "0.0.1",
4 "keywords": ["react", "create-react-app", "template"],
5 "description": "The cra-template-junior template for create-react-app",
6 "repository": {
7 "type": "git",
8 "url": "https://github.com/eyupucmaz/cra-template-junior.git"
9 },
10 "license": "MIT",
11 "engines": {
12 "node": ">=14"
13 },
14 "bugs": {
15 "url": "https://github.com/eyupucmaz/cra-template-junior/issues"
16 },
17 "files": ["template", "template.json"]
18}

You should add your own name and own github repository. Also there is a last thing you must have, your project name should start with cra-template- for me cra-template-junior 🔴

Publishing Template

Finally we have own template but can not able to use. We should publish our template to npm package manager. Open npmjs.com and create a profile. We'll use account to publish our template.

Open a console and login to npm. Use your username and password for login.

1npm login

If you successfully logged in you should see a message on console. Final part is publishing to do that just type npm publish and press enter. 🚀

Congrats 👏 You published your own cra template 🙂 Let’s use our template for our new project 🙂

1npx create-react-app --template [templatename] my-project

The [templatename] is what you named after cra-template- for me is junior so my actual template name is cra-template-junior so you should use your own. Feel free to use mine as well.

My Npm Package

Thanks for reading 🙂

Also check it out