module関係のApplication errorの記事です。
勉強用にHerokuに初デプロイした際に発生した事象をまとめました。
環境
ProductName: Mac OS X
ProductVersion: 10.13.6
BuildVersion: 17G14033
yutakaf@mi ~/Desktop/git/weather_web main heroku --version
heroku/7.46.2 darwin-x64 node-v12.16.2
yutakaf@mi ~/Desktop/git/weather_web main node -v
v12.18.4
素直にheroku logs --tail
を入力しよう。
yutakaf@mi ~/Desktop/git/weather_web main heroku logs --tail
2020-10-27T04:13:34.590447+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
2020-10-27T04:13:34.590448+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2020-10-27T04:13:34.590448+00:00 app[web.1]: requireStack: [ '/app/src/app.js' ]
2020-10-27T04:13:34.590448+00:00 app[web.1]: }
2020-10-27T04:13:34.601505+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-10-27T04:13:34.601726+00:00 app[web.1]: npm ERR! errno 1
2020-10-27T04:13:34.606489+00:00 app[web.1]: npm ERR! weather_web@1.0.0 start: `node src/app.js`
2020-10-27T04:13:34.606605+00:00 app[web.1]: npm ERR! Exit status 1
2020-10-27T04:13:34.606716+00:00 app[web.1]: npm ERR!
2020-10-27T04:13:34.606828+00:00 app[web.1]: npm ERR! Failed at the weather_web@1.0.0 start script.
2020-10-27T04:13:34.606911+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-10-27T04:13:34.611862+00:00 app[web.1]:
2020-10-27T04:13:34.612012+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-10-27T04:13:34.612101+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-10-27T04_13_34_607Z-debug.log
2020-10-27T04:13:34.661115+00:00 heroku[web.1]: Process exited with status 1
code: 'MODULE_NOT_FOUND'でmoduleが見つかっていないと言われている。
package.jsonでmoduleのインストール設定がdevDependenciesになっているので当たり前だぞ。
起動確認でもcrashedと言われている。
✘ yutakaf@mi ~/Desktop/git heroku ps --app rac-weather-application
Free dyno hours quota remaining this month: 550h 0m (100%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): npm start (1)
web.1: crashed 2020/10/27 13:17:14 +0900 (~ 5m ago)
変更前
{
"name": "weather_web",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node src/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"hbs": "^4.1.1",
"nodemon": "^2.0.6",
"request": "^2.88.2"
}
}
変更後
{
"name": "weather_web",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node src/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.6"
},
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"hbs": "^4.1.1",
"request": "^2.88.2"
}
}
※1 ちなみにrequest moduleは現在非推奨だぞ。
※2 そもそも、どのモジュールをちゃんと使うか考えていなかったので、npm install --save--dev <package>
していたのが原因。
アクセスキーを環境変数から取得している。
githubでpublic設定にしてみたかったので、アクセスキーは公開できない。
なのでコード上では、環境変数から取得する形式をとっていた。
こちらのエラーはモジュール問題を解決する前に解消してしまったため(たぶん)ログにて出力されていないが、おそらく、うまくいっていない場合には、エラーがでたと思われる。
下記のように設定した。
✘ yutakaf@mi ~/Desktop/git/weather_web main heroku config:set --app rac-weather-application WEATHER_ACCESS_KEY='WEATHER_ACCESSのアクセスキー'
Setting WEATHER_ACCESS_KEY and restarting ⬢ rac-weather-application... done, v4
WEATHER_ACCESS_KEY: WEATHER_ACCESSのアクセスキー
yutakaf@mi ~/Desktop/git/weather_web main heroku config:set --app rac-weather-application MAPBOX_ACCESS_KEY='MAPBOX_ACCESSのアクセスキー'
Setting MAPBOX_ACCESS_KEY and restarting ⬢ rac-weather-application... done, v5
MAPBOX_ACCESS_KEY: MAPBOX_ACCESSのアクセスキー
起動ログ
2020-10-27T05:01:17.934935+00:00 heroku[web.1]: Idling
2020-10-27T05:01:17.937105+00:00 heroku[web.1]: State changed from up to down
2020-10-27T05:01:19.323569+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-10-27T05:01:19.460035+00:00 heroku[web.1]: Process exited with status 143
2020-10-27T05:12:04.440395+00:00 heroku[web.1]: Unidling
2020-10-27T05:12:04.442228+00:00 heroku[web.1]: State changed from down to starting
2020-10-27T05:12:07.737901+00:00 heroku[web.1]: Starting process with command `npm start`
2020-10-27T05:12:10.699194+00:00 app[web.1]:
2020-10-27T05:12:10.699212+00:00 app[web.1]: > weather_web@1.0.0 start /app
2020-10-27T05:12:10.699213+00:00 app[web.1]: > node src/app.js
2020-10-27T05:12:10.699213+00:00 app[web.1]:
2020-10-27T05:12:11.246046+00:00 app[web.1]: Server is up on port 23085.
2020-10-27T05:12:11.944720+00:00 heroku[web.1]: State changed from starting to up
2020-10-27T05:12:13.551783+00:00 heroku[router]: at=info method=GET path="/" host=rac-weather-application.herokuapp.com request_id=d778277e-47cc-4e1d-b7e1-94210990b33b fwd="153.229.157.158" dyno=web.1 connect=0ms service=60ms status=200 bytes=982 protocol=https
2020-10-27T05:12:13.911402+00:00 heroku[router]: at=info method=GET path="/css/styles.css" host=rac-weather-application.herokuapp.com request_id=ce338878-68d3-4356-a04a-d7a3375425da fwd="153.229.157.158" dyno=web.1 connect=0ms service=32ms status=200 bytes=1135 protocol=https
2020-10-27T05:12:14.130888+00:00 heroku[router]: at=info method=GET path="/js/app.js" host=rac-weather-application.herokuapp.com request_id=db28448f-e70f-4059-b1d1-4cc9572a17d0 fwd="153.229.157.158" dyno=web.1 connect=0ms service=4ms status=200 bytes=951 protocol=https
2020-10-27T05:12:14.366065+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=rac-weather-application.herokuapp.com request_id=38ea6d92-4f3f-4cef-bcec-dea094db7ede fwd="153.229.157.158" dyno=web.1 connect=0ms service=15ms status=200 bytes=815 protocol=https
2020-10-27T05:12:21.452041+00:00 heroku[router]: at=info method=GET path="/weather?address=Tokyo" host=rac-weather-application.herokuapp.com request_id=f83c9643-bc93-4f29-8857-b5aa32e691f1 fwd="153.229.157.158" dyno=web.1 connect=3ms service=600ms status=200 bytes=466 protocol=https
とりあえず、テストデプロイまで完了
WeatherApplication ※起動まで時間かかる。
github ※コードはこっち。
WeatherApplication内にgithubのリンク貼るとか、いろいろと思うことはありますが、環境付きでのエラー記事は貴重かと思ったので記事化しました。
References
【Node】dotenvで環境変数を設定する
package.jsonにおけるdependenciesとdevDependenciesの違い(超シンプルに)
Node.jsでのHerokuチュートリアル前編
[heroku] 環境変数の操作
WEATHERのAPI(位置情報→天気取得)
MAPBOXのAPI(住所→位置情報取得)