いつもお世話になっております。
標記の件につきまして、質問させていただきます。
現在、Windows10Pro 64bit上で、Docker for Windowsをインストールして開発を行っています。
エディタはVisual Studio Code(ver.1.41.1)を使っており、デバッグのために拡張機能として
「Debugger for Chrome(ver.4.12.3)」をインストールしました。
React(ver.16.12.0)を使い始めたのですが、react-scripts(ver.3.3.0)の
create-react-appコマンドを使って、最初に構築される雛形のスクリプトに対して
ブレークポイントを設定してデバッグを開始したところ、
赤い丸印がグレーになってそちらにカーソルを合わせると
VScode
1Breakpoint set but not yet bound
というメッセージが表示されて、ブレークポイントで処理が止まりませんでした。
js
1// index.js 2 3import React from 'react'; 4import ReactDOM from 'react-dom'; 5import './index.css'; 6import App from './App'; 7import * as serviceWorker from './serviceWorker'; 8 9ReactDOM.render(<App />, document.getElementById('root')); 10 11console.log('test1'); 12console.log('test2'); 13 14serviceWorker.unregister();
スクリプト自体はブレークポイントの確認をするために2箇所で「console.log」を入れていますが、
それ以外は初期の雛形のままのシンプルなもので、以下URLにアクセスすることで問題なく表示できています。
http://localhost:3000/
以下から詳細になりますが、Chromeの拡張機能の方の設定ファイルは以下のように記述しています。
json
1// launch.json 2{ 3 "version": "0.2.0", 4 "configurations": [ 5 { 6 "name": "Launch Chrome", 7 "type": "chrome", 8 "request": "launch", 9 "url": "http://localhost:3000", 10 "webRoot": "${workspaceRoot}/src", 11 "userDataDir": "${workspaceRoot}/.vscode/chrome", 12 "runtimeArgs": ["--remote-debugging-port=9222"], 13 "sourceMapPathOverrides": { 14 "webpack:///src/*": "${webRoot}/*" 15 } 16 } 17 ] 18}
また、スクリプトの方の「package.json」は以下のようになっています。
json
1// package.json 2{ 3 "name": "test", 4 "version": "0.1.0", 5 "private": true, 6 "dependencies": { 7 "@testing-library/jest-dom": "^4.2.4", 8 "@testing-library/react": "^9.3.2", 9 "@testing-library/user-event": "^7.1.2", 10 "react": "^16.12.0", 11 "react-dom": "^16.12.0", 12 "react-scripts": "3.3.0" 13 }, 14 "scripts": { 15 "start": "react-scripts start", 16 "build": "react-scripts build", 17 "test": "react-scripts test", 18 "eject": "react-scripts eject" 19 }, 20 "eslintConfig": { 21 "extends": "react-app" 22 }, 23 "browserslist": { 24 "production": [ 25 ">0.2%", 26 "not dead", 27 "not op_mini all" 28 ], 29 "development": [ 30 "last 1 chrome version", 31 "last 1 firefox version", 32 "last 1 safari version" 33 ] 34 } 35}
コンパイルも問題なく完了して、以下のコマンドでサーバも正常に立ち上がります。
Bash
1npm run start
この段階ではVScodeで設定したブレークポイントは赤印のまま問題ないように見えます。
しかし、VScodeでデバッグの開始ボタンを押すと、Chromeの別ウインドウが立ち上がって
ページ自体は正常に表示されるのですが、ブレークポイントの赤印がグレーになって、
そこで止まらずにコンソールに「test1」「test2」が続けて出力されてしまいます。
いろいろと原因調査したところ、nodeの起動時に「--debug」というオプションを付ければ
よさそうなところまでは何となく分かったのですが、
Bash
1node --debug
react-scriptsの方では内部でどのようにnodeを起動させているのか分かりませんでした。
こちらについていくつか調べていた時に以下のサイトが見つかりました。
https://create-react-app.dev/docs/debugging-tests/
こちらを参考にして、package.jsonのscriptsに「test:debug」を追加してみました。
json
1// package.json 2 3 "scripts": { 4 "start": "react-scripts start", 5 "build": "react-scripts build", 6 "test": "react-scripts test", 7 "test:debug": "react-scripts --inspect-brk test --runInBand --no-cache", 8 "eject": "react-scripts eject" 9 },
そして、コマンドを入力したところ以下のようなメッセージが表示されました。
Bash
1root:/var/www/test# npm run test:debug 2 3> test@0.1.0 test:debug /var/www/test 4> react-scripts --inspect-brk test --runInBand --no-cache 5 6Debugger listening on ws://127.0.0.1:9229/20a8af2c-ed6e-4ea4-8ace-339d6bd91ac1 7For help, see: https://nodejs.org/en/docs/inspector
この状態でVScodeのデバッグを開始すると、Chromeが立ち上がってブレークポイントの赤印が
グレーになるのは変わらず、今度はページの方が正常に表示されませんでした。
Bash
1# curl -i http://localhost:3000 2 3curl: (7) Failed to connect to localhost port 3000: Connection refused
CURLでチェックしたところ、サーバ自体が正常に立ち上がっていないようです。
package.jsonに追加した「react-scripts --inspect-brk test --runInBand --no-cache」に
問題があるかと思われ、react-scriptsのソースを追ってみましたが、
nodeの「--debug」オプションを付与できるような箇所は見つけられませんでした。
つきましては、react-scriptsを利用したスクリプトで、VScodeとChromeの拡張機能を使って
ブレークポイントが機能するデバッグを行う方法について、何かアドバイスがございましたら
よろしくお願いいたします。
あなたの回答
tips
プレビュー