Create React App TypeScript Template
This package is the official TypeScript template for Create React App (CRA), a widely adopted command-line interface for bootstrapping single-page React applications. It provides a pre-configured setup with TypeScript out of the box, including a comprehensive build pipeline, a development server with hot-reloading, a testing framework (Jest), and linting (ESLint). Currently, CRA itself (and by extension, this template) is effectively in maintenance mode, with the last significant feature update being `react-scripts@5.0.1` in April 2022, primarily for React 18 compatibility. While stable and well-suited for existing projects or simple new ones, many developers are now opting for alternative tools like Vite, Next.js, or Remix for new projects due to their faster build times and more modern approaches to development.
Common errors
-
Module not found: Error: Can't resolve 'react' in '...' or similar 'Cannot find module'
cause The core `react` package or one of its dependencies is not correctly installed or symlinked in the project's `node_modules` directory, or there's an issue with the package manager cache.fixNavigate to your project directory and run `npm install` or `yarn install`. If the problem persists, try clearing your package manager cache (`npm cache clean --force` or `yarn cache clean`), then delete `node_modules` and `package-lock.json` (or `yarn.lock`), and reinstall. -
TypeError: Cannot read properties of undefined (reading 'process') at Object.<anonymous> (../node_modules/some-lib/index.js:X:Y)
cause This error frequently occurs in Create React App v5 projects (which use webpack 5) when a third-party library assumes the presence of Node.js global variables like `process` or `Buffer` in a browser environment. Webpack 5 no longer automatically polyfills these by default.fixThis issue is complex to fix without ejecting CRA. The best approach is to check if an updated version of the problematic library exists that is compatible with Webpack 5. If not, you might need to find an alternative library or, as a last resort, use tools like `customize-cra` (with the associated risks of not having official support) to add webpack polyfills. -
Error: `react-scripts` is not found. Did you mean to run `npm install` or `yarn install`?
cause The `react-scripts` package, which provides all the executable scripts (`start`, `build`, `test`) for Create React App, is either missing from your `node_modules` or is not listed in your project's `package.json` dependencies.fixEnsure `react-scripts` is present in your `package.json` under `dependencies` and run `npm install` or `yarn install` in your project's root directory. If you've just created the project, verify that the `create-react-app` command completed without errors.
Warnings
- breaking Create React App v5.0.0 (`react-scripts@5.x.x`) introduced significant dependency upgrades including webpack 5, Jest 27, ESLint 8, and PostCSS 8. These major version bumps in core tooling can lead to compatibility issues with older custom configurations (e.g., `config-overrides.js`) or specific third-party libraries that rely on older versions of these tools.
- breaking CRA v5.0.1 (`react-scripts@5.0.1`) brought improved compatibility with React 18, which introduced the new root API (`ReactDOM.createRoot`). Projects migrating from CRA 4.x or earlier to 5.x *must* update their `src/index.tsx` file to use `createRoot` instead of the deprecated `ReactDOM.render` for concurrent mode features and to avoid console warnings.
- breaking CRA v4.0.0 (`react-scripts@4.x.x`) enabled the new JSX transform introduced in React 17. This change means that `import React from 'react';` is no longer strictly required at the top of every file that uses JSX, as the React runtime is automatically injected by the compiler.
- gotcha When updating an existing Create React App project, it is strongly recommended to update `react-scripts` using `npm install --save --save-exact react-scripts@latest` or `yarn add --exact react-scripts@latest`. Using `--save-exact` or `--exact` ensures that the `react-scripts` package and its tightly coupled internal dependencies (like Webpack, Babel) are installed at precise, tested versions, preventing potential dependency mismatches and build failures.
- gotcha Create React App, and thus projects created with this template, requires a Node.js version of 14 or higher. Using older Node.js versions can lead to installation failures, build errors, or runtime issues due to incompatibility with updated package dependencies.
Install
-
npm install cra-template-typescript -
yarn add cra-template-typescript -
pnpm add cra-template-typescript
Imports
- App (component)
const App = require('./App');import './App.css'; import React from 'react'; // Or omit in newer CRA with new JSX transform function App() { return <div>Hello world</div>; } - useState (React Hook)
const { useState } = require('react');import React, { useState } from 'react'; function MyComponent() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; } - React.FC (Functional Component type)
import { FC } from 'react';import React from 'react'; interface MyComponentProps { name: string; } const MyComponent: React.FC<MyComponentProps> = ({ name }) => { return <div>Hello, {name}</div>; };
Quickstart
npx create-react-app my-ts-app --template typescript
cd my-ts-app
npm start
// To build your application for production:
// npm run build
// Example of a basic component structure in src/App.tsx after creation:
// import React from 'react';
// import './App.css';
// function App() {
// return (
// <div className="App">
// <header className="App-header">
// <p>Edit <code>src/App.tsx</code> and save to reload.</p>
// </header>
// </div>
// );
// }
// export default App;