「npx create-react-app」コマンドでエラー「ERESOLVE unable to resolve dependency tree」

  1. エラー内容
  2. package.json の修正
  3. 失敗した部分のコマンドを再実行
  4. npm start

エラー内容

発生したエラーは下記の通り。

% npx create-react-app my-new-app

Creating a new React app in /Users/username/React/my-new-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


added 1324 packages in 16s

268 packages are looking for funding
  run `npm fund` for details

Initialized a git repository.

Installing template dependencies using npm...
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree
npm error
npm error While resolving: my-new-app@0.1.0
npm error Found: react@19.0.0
npm error node_modules/react
npm error   react@"^19.0.0" from the root project
npm error
npm error Could not resolve dependency:
npm error peer react@"^18.0.0" from @testing-library/react@13.4.0
npm error node_modules/@testing-library/react
npm error   @testing-library/react@"^13.0.0" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.
npm error
npm error
npm error For a full report see:
npm error /Users/username/.npm/_logs/2025-01-26T21_38_14_756Z-eresolve-report.txt
npm error A complete log of this run can be found in: /Users/username/.npm/_logs/2025-01-26T21_38_14_756Z-debug-0.log
`npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0` failed

エラーの内容としては、依存関係の解決に失敗したとのこと。

一番下の部分で「npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0」というコマンドが失敗したと記述があります。

後でこの「npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0」の部分を再実行します。

package.json の修正

途中まで作成された my-new-app ディレクトリに入り、package.json ファイルを修正します。

# package.json

"dependencies": {
    "cra-template": "1.2.0",
    "react": "^19.0.0",
    "react-dom": "^19.0.0",
    "react-scripts": "5.0.1"
  },

上記のように dependencies となっている中の「19.0.0」を「18.0.0」に変更します。

# package.json

"dependencies": {
    "cra-template": "1.2.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-scripts": "5.0.1"
  },

これでファイルを保存します。

失敗した部分のコマンドを再実行

ターミナル上でも「my-new-app」ディレクトリに移動し、先ほど失敗していた「npm install –no-audit –save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0」を実行します。

% cd my-new-app
% npm install --no-audit --save @testing-library/jest-dom@^5.14.1 @testing-library/react@^13.0.0 @testing-library/user-event@^13.2.1 web-vitals@^2.1.0

added 47 packages, and changed 4 packages in 6s

272 packages are looking for funding
  run `npm fund` for details

特にエラーは起きませんでした。

npm start

react を起動します。

% npm start
Compiled successfully!

You can now view my-new-app in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://192.168.1.9:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

webpack compiled successfully

無事に起動できました。

Mac に React.js をインストールする方法

  1. Homebrew で node をインストール
  2. ディレクトリを作成して create-react-app をインストール
  3. React プロジェクトの作成
  4. React プロジェクトの実行

1. Homebrew で node をインストール

% brew update
% brew install node
% node --version
v16.10.0

2. ディレクトリを作成して create-react-app をインストール

% mkdir React
% cd React
% 

コマンド「npm install -g create-react-app」を実行します。

% npm install -g create-react-app
npm WARN deprecated tar@2.2.2: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.

added 67 packages, and audited 68 packages in 4s

4 packages are looking for funding
  run `npm fund` for details

2 high severity vulnerabilities

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

いくつか警告が出ている様ですがとりあえず無視して進みます。

3. React プロジェクトの作成

例として「awesome-project」という React プロジェクトを作成するとします。

コマンド「create-react-app awesome-project」を実行します。

% create-react-app awesome-project

Creating a new React app in /Users/ユーザー名/React/awesome-project.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


added 1934 packages, and audited 1935 packages in 2m

150 packages are looking for funding
  run `npm fund` for details

10 moderate severity vulnerabilities

To address all issues, run:
  npm audit fix

Run `npm audit` for details.

Initialized a git repository.

Installing template dependencies using npm...

added 56 packages, and audited 1991 packages in 7s

151 packages are looking for funding
  run `npm fund` for details

10 moderate severity vulnerabilities

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.
Removing template package using npm...


removed 1 package, and audited 1990 packages in 3s

151 packages are looking for funding
  run `npm fund` for details

10 moderate severity vulnerabilities

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

Created git commit.

Success! Created awesome-project at /Users/ユーザー名/React/awesome-project
Inside that directory, you can run several commands:

  npm start
    Starts the development server.

  npm run build
    Bundles the app into static files for production.

  npm test
    Starts the test runner.

  npm run eject
    Removes this tool and copies build dependencies, configuration files
    and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

  cd awesome-project
  npm start

Happy hacking!
% 

4. React プロジェクトの実行

% cd awesome-project
% npm start

「awesome-project」フォルダの中に入り、コマンド「npm start」を実行します。

ターミナルの表示が下記に変わり、ブラウザが起動します。

Compiled successfully!

You can now view awesome-project in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://192.168.3.3:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

ブラウザで上記の表示が出れば無事起動できています。

Control+C でサーバーを停止します。