← cd ../blog

Generate React CLI: create React component files in one command

In one sentence

Generate React CLI is a program that you use from the terminal. It creates, in one command, the basic files of a React component, that is, a reusable piece of a web page.

What it is and what it’s for

React is a technology for building the interface of websites. A site made with React is made of pieces called components. A component is one part of the interface, for example a button, a header, or a box with a title.

Each component usually has several files. The code goes in a .js file. The look goes in a style sheet. A test file, that is, an automatic check, verifies that the component works.

Creating these files by hand is repetitive. You have to create the folders, use the same names, and link the files to each other. A wrong name can break the link.

Generate React CLI does this work for you. You run it from the project folder and type the name of the component. The program creates folders and files, always with the same structure.

The first time you start the program, it asks you a few questions about the project, for example whether you use TypeScript, a variant of JavaScript with types. The answers go into the file generate-react-cli.json, so you don’t have to repeat them every time.

The program uses templates: starting texts that it uses to create the files. You can use the built-in ones or replace them with your own. You can also define different types of components, for example pages.

A concrete example

Let’s create an empty folder with a test project and generate the components Header, Button, and Card with a single command.

This can help if you are preparing the interface of a small website and you don’t want to write the same file structure three times. At the end you will have three folders in src/components, each with the component, the style sheet, and the test.

What you will see in the terminal (don’t type this: it is only there for comparison):

import styles from './Button.module.css';
const Button = () => (
  <div className={styles.Button} data-testid="Button">
    Button Component
  </div>
);
export default Button;

Who it’s for (and who it isn’t)

It is worth trying if you fit one of these profiles:

Project card

What you need to try it, and what you DON’T need

You need three things: a terminal, Node.js, and git. The terminal is the window where you type commands. Node.js runs the program: you need version 22 or higher, with npm 10 or higher, which installs the support programs of the project.

The project can be downloaded from https://github.com/arminbro/generate-react-cli, and the first step of this walkthrough downloads it with git.

Python is not needed: the project is written in JavaScript, the language of websites, and it runs with Node.js. You don’t need keys or accounts. You only need an internet connection.

Let’s try it step by step

This walkthrough has two steps. The first installs the project. The second is doing the exercise.

Step 1: install the project

What we are doing: we download the project and install its dependencies, that is, the support programs it needs.

Type this in the terminal, from a folder of your choice:

git clone https://github.com/arminbro/generate-react-cli
cd generate-react-cli
git checkout 280e99a
npm install
cd ..
mkdir progetto-demo
cd progetto-demo

What you will see in the terminal:

Cloning into 'generate-react-cli'...
Note: switching to '280e99a'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this

What you will see: the first lines of the output are in English. Cloning means that the copy is in progress. The rest of the output is normal. The snippet above shows only the first lines: the real output is longer, but the first lines match.

How to tell it went well: there are no error messages and the terminal is ready to receive a new command again.

Step 2: do the exercise

What we are doing: we create an empty test folder, write the project’s choices into the file generate-react-cli.json, and run the program for the three components.

Type this in the terminal, from the progetto-demo folder:

npm init -y
echo '{"usesTypeScript":false,"usesStyledComponents":false,"usesCssModule":true,"cssPreprocessor":"css","testLibrary":"Testing Library","component":{"default":{"path":"src/components","withLazy":false,"withStory":false,"withStyle":true,"withTest":true}}}' > generate-react-cli.json
node ../generate-react-cli/bin/generate-react.js component Header Button Card
ls -R src
cat src/components/Button/Button.js

What you will see in the terminal (don’t type this: it is only there for comparison):

Wrote to /work/progetto-demo/package.json:

{
  "name": "progetto-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
✓ Created 3 files in src/components/Header
  ├── ✓ Header.js
  ├── ✓ Header.module.css
  └── ✓ Header.test.js

✓ Created 3 files in src/components/Button
  ├── ✓ Button.js
  ├── ✓ Button.module.css
  └── ✓ Button.test.js

✓ Created 3 files in src/components/Card
  ├── ✓ Card.js
  ├── ✓ Card.module.css
  └── ✓ Card.test.js
src:
components

src/components:
Button
Card
...

What you will see: the program answers in English. Created 3 files means “3 files created”. You will also see the list of folders and the code of a component. The output above is cut short: the real one is more complete.

How to tell it went well: inside src/components there are three folders: Header, Button, and Card. In each of them you will find three files, for example for Header: Header.js, Header.module.css, and Header.test.js.

What happened behind the scenes

The test folder becomes a Node.js project. The npm command creates a file package.json, which contains the name of the project and its basic data.

The file generate-react-cli.json contains the choices: no TypeScript, style files with CSS module, tests with Testing Library, a library for writing component tests, and components inside src/components. Normally the program would ask you these choices the first time you start it. Here we write them directly into the file, and the reason is explained in the section on limits. A CSS module is a style sheet that applies only to the component it belongs to.

The node command starts the program from the downloaded folder, without installing it on the system. The word component and the three names tell the program what to create.

For each name, the program creates a folder and writes the files in it. It uses the same procedure for all three. It chooses the JavaScript template, because TypeScript is turned off, and the template with the CSS module style sheet.

Each file starts from a built-in template. The Button component links its own style sheet and shows an element with its name, ready to be edited.

If you want to see what would be created without writing anything, there is the --dry-run option. You add it to the command, after the names of the components.

Things to keep in mind

You need Node.js 22 or higher. With an older version, the program ends with an error. Check the installed version before you start.

The first questions need someone to answer them. If the program receives no answers, the choices file is not created and an error appears. For this reason, the walkthrough creates that file with a command, instead of the questions.

The component type is written with the equals sign. Write --type=page. With a space, --type page, the program answers with an error.

The project has no test or build commands. There is no default command for testing, or for the build, that is, the step that prepares the program for use. If you change it, you will not find an automatic check already ready.

Where to start reading the code

The first file to open is bin/generate-react.js. It is the file you ran with node, so that is where the program starts.

Glossary

A note on the test

This walkthrough was tested on Debian 12 in WSL2, that is, Linux inside Windows, with Node.js 22 and npm 10. Native Windows, that is, without WSL2, and macOS were not tested.