Setting Up a Local Development Environment
Relevant source files
The following files were used as context for generating this wiki page:
Setting up a local development environment is the first step to contributing to the React project. The development of React happens openly on GitHub, and the project relies on community contributions for bug fixes and improvements. This guide provides a comprehensive overview of how to clone the repository, install dependencies, build the necessary packages, and run tests to validate your changes.
The main purpose of the repository is to continue evolving React core, making it faster and easier to use. The local setup is designed to support development on React itself, its associated packages like React DOM, and the React DevTools. Following these instructions will ensure you have a functional environment for building and testing any proposed changes.
Sources: README.md:37-40, CONTRIBUTING.md
Initial Setup
The initial setup involves cloning the repository and installing all required dependencies using Yarn.
This diagram shows the initial one-time setup process for a contributor.
- Clone the Repository:
git clone git@github.com:facebook/react.git - Navigate into the Directory:
cd react - Install Dependencies: The project uses Yarn for dependency management.
yarn install
Sources: packages/react-devtools/CONTRIBUTING.md:6-11
Building React from Source
After setting up the repository, you need a local build of the React packages. This is a prerequisite for running tests or using development tools like React DevTools. You have two primary options: downloading a pre-built version from CI or building it from source locally.
Build Options
This flowchart illustrates the decision process for obtaining a local React build.
Option 1: Download Pre-built Artifacts from CI (Fastest)
For most development work, especially on React DevTools, you can download the latest experimental build directly from the CI. This is faster than a full local build.
To download the latest build from the main branch:
cd scripts/release
yarn install
./download-experimental-build.js --commit=main
Sources: packages/react-devtools/CONTRIBUTING.md:17-22
You can also download artifacts for a specific commit by using the --commit flag with the commit SHA. This is useful for preparing a next or experimental release.
scripts/release/prepare-release-from-ci.js -r stable --commit=<commit-sha>
Sources: scripts/release/README.md:85-87
Option 2: Build from Source
If your changes involve modifications to the React core packages (like react or react-dom), you must build them from source.
Run the following command from the repository root:
yarn build-for-devtools
This script creates a local build of React and its dependencies, which is necessary for tools like DevTools to function correctly with your local changes.
Sources: packages/react-devtools/CONTRIBUTING.md:24-27
Running and Testing
With a local build in place, you can run various test applications (fixtures) and execute the test suites to validate your changes.
Common Development Scripts
The project uses a set of common scripts, typically defined in the package.json of individual fixtures or packages.
| Script | Description | Example Location |
|---|---|---|
yarn start | Runs the app in development mode, usually on http://localhost:3000. | fixtures/owner-stacks/README.md:7-10 |
yarn test | Launches the test runner in interactive watch mode. | fixtures/owner-stacks/README.md:13-15 |
yarn build | Builds the app for production, optimizing for performance. | fixtures/owner-stacks/README.md:18-21 |
Sources: fixtures/owner-stacks/README.md:5-21
Testing React DevTools
Testing changes to React DevTools has a specific workflow.
Using the Test Shell (Fastest)
Most DevTools changes can be tested using a dedicated test shell. This requires two terminal sessions.
- Run DevTools in DEV mode:
cd packages/react-devtools-inline yarn start - Run the test shell:
cd packages/react-devtools-shell yarn start
This will open a test application at http://localhost:8080 that hot-reloads with your DevTools changes.
Sources: packages/react-devtools/CONTRIBUTING.md:32-44
Using the Browser Extension
For changes that must be tested within the actual browser extension (e.g., "named hooks" feature), you need to build and launch a test instance of Chrome.
cd packages/react-devtools-extensions
yarn build:chrome:local && yarn test:chrome
This command builds a local version of the Chrome extension and opens a new Chrome window with it pre-installed. You can pass a --url argument to test a specific page.
Sources: packages/react-devtools/CONTRIBUTING.md:46-54
Connecting Standalone DevTools to a Web App
For debugging in browsers like Safari or in an embedded webview, you can use the standalone react-devtools package.
The following diagram shows the sequence for connecting the standalone DevTools to a running web application.
To achieve this:
- Install
react-devtoolsglobally or vianpx.# NPM npm install -g react-devtools - Run the DevTools application from your terminal.
react-devtools - Add the following script tag to the
<head>of your HTML file. It must be the very first script.<script src="http://localhost:8097"></script>
Sources: packages/react-devtools/README.md:10-14, packages/react-devtools/README.md:83-90
Unit Tests
To run the core unit tests for DevTools, use the following command after ensuring React has been built locally:
yarn test-build-devtools
Sources: packages/react-devtools/CONTRIBUTING.md:60-62
Build System
The React repository uses a sophisticated build system to create various package formats (UMD, CJS, etc.) and to bundle applications and extensions.
- Webpack: Used for bundling front-end assets, such as the DevTools extension. The configuration is managed in files like
webpack.config.js. The build script executes Webpack via its CLI.
Sources: packages/react-devtools-extensions/build.js:34-48 - Release Scripts: A collection of scripts in
scripts/releasemanages the process of building, preparing, and publishing releases. These scripts handle versioning, dependency updates, and packaging for NPM.
Sources: scripts/release/README.md:13-26 - Size Monitoring: The
dangerfile.jsscript runs during CI to compare build artifact sizes between the base and head commits. It reports critical or significant size changes in pull requests to prevent unintended bundle size increases.
Sources: dangerfile.js:30-34, dangerfile.js:150-176
Summary
Setting up a local development environment for React involves cloning the repository, installing dependencies with Yarn, and creating a local build of the React packages. Depending on the nature of the contribution, developers can either download pre-built artifacts from CI for a faster setup or build from source to test changes in React's core. The repository includes numerous test fixtures and dedicated testing scripts for validating changes across React, React DOM, and the Developer Tools, ensuring that contributions are robust and well-tested.