kdnakt blog

hello there.

React Hooksを学ぼうとしたらReactアプリをyarn startで起動できなかった

Reactの最新情報をキャッチアップしようとしたら初っ端からつまづいたのでメモしておく。

 

 

[React Hooksを学ぶ]

Reactは昔少し触っただけで、最近のアップデートを全く取り入れていない状況だった。そんな状況ながら、細々とReact Nativeでアプリを作っていたので、なんとなくReactの最新情報をチラ見してはいた。

 

そして2018年末くらいからReact Hooksの名前を聞くようになり、いつかちゃんと勉強しよう、と思っていたところ、キャッチアップに良さそうな本を見つけた。

booth.pm

note.mu

 

という訳で、先週半ばからReact Hooksを学んでいる。

github.com

 

[yarn startに失敗する]

本に記載されている通りに次のようにyarnを利用してReactアプリを初期化した。

$ yarn create react-app sample --typescript

そして

$ cd sample
$ yarn start

でReactアプリを起動しようとしたところ、次のようなエラーが出た。

f:id:kidani_a:20190805012835p:plain

 

$ yarn start
yarn run v1.12.3
$ react-scripts start

There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "babel-jest": "^24.8.0"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-jest was detected higher up in the tree:

  /Users/akito/node_modules/babel-jest (version: 22.0.4) 

Manually installing incompatible versions is known to cause hard-to-debug issues.

If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "babel-jest" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if /Users/akito/node_modules/babel-jest is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls babel-jest in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed babel-jest.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

 

確認したところ、package.jsonにはbabel-jestへの直接的な依存関係は宣言されていなかった。 何故かホームディレクトリにbabel-jestの古いバージョン(22.0.4)がインストールされているが、react-scriptsが必要としているバージョンは24.x.x以上ということでエラーになっているらしい。

 

この辺りの細かいyarnの仕様を確認しておくと色々勉強になりそうだったが、React Hooksを学ぶのが本筋なので、一旦yarnの挙動についてはスルーすることに。

Reactアプリを初期化したディレクトリにバージョン24を新規でインストールしてくれればそれで問題ないはずなのに、何故ホームディレクトリにインストールされている古いバージョンが影響するのだろう……と疑問に思いつつ、次のコマンドで不要と思しきディレクトリを削除する。

$ rm -r /Users/akito/node_modules/babel-jest

 

すると、無事にアプリを起動できるようになった。

$ yarn start

Compiled successfully!

You can now view sample in the browser.

  Local:            http://localhost:3000/
  On Your Network:  http://192.168.xxx.xxx:3000/

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

 

ブラウザでURLにアクセスすると、無事以下のような画面が表示された。

f:id:kidani_a:20190805232852p:plain

 

[まとめ]

  • Effective React HooksでReact Hooksを学んでいる
  • yarn startでサーバが起動しないときはエラーメッセージの指示に従って修正しよう