Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js'#4
Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js'#4Copilot wants to merge 2 commits into
Cannot find module 'whatsapp-web.js'#4Conversation
…b.js' error Co-authored-by: eeea2222 <209839587+eeea2222@users.noreply.github.com>
Cannot find module 'whatsapp-web.js'
|
ok |
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent the WhatsApp bot from crashing on fresh clones by ensuring Node.js dependencies are installed before the Node bot process is started.
Changes:
- Added
ensure_npm_deps()to auto-runnpm installwhennode_modules/is missing. - Updated
ensure_bot_file()to invokeensure_npm_deps()whenwhatsapp_bot.jsexists. - Introduced a repository
.gitignoreto exclude Node/Python/runtime artifacts from version control.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| app.py | Adds startup-time npm dependency installation and wires it into bot startup checks. |
| .gitignore | Ignores Node dependencies, WhatsApp session/cache data, Python artifacts, and runtime files. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if result.returncode == 0: | ||
| log.info("npm install başarılı.") | ||
| else: | ||
| output = (result.stdout.strip() + "\n" + result.stderr.strip()).strip() | ||
| log.error(f"npm install başarısız (kod {result.returncode}): {output}") | ||
| except FileNotFoundError: | ||
| log.error("npm bulunamadı. Node.js kurulu olduğundan emin olun.") | ||
| except subprocess.TimeoutExpired: | ||
| log.error("npm install zaman aşımına uğradı.") |
There was a problem hiding this comment.
ensure_npm_deps() logs npm install failures (non-zero exit, timeout, or missing npm) but still returns normally, so callers will proceed to start the bot and hit the same “Cannot find module …” crash. Consider making this function fail-fast (raise an exception or return a boolean that ensure_bot_file()/bot_start checks) so startup is aborted when dependencies cannot be installed.
| log.error(f"whatsapp_bot.js bulunamadı: {bot_path}") | ||
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | ||
| else: | ||
| ensure_npm_deps() |
There was a problem hiding this comment.
ensure_bot_file() logs that whatsapp_bot.js is missing but still returns bot_path, and /api/bot/start calls bot.start() unconditionally afterward. This leads to a confusing follow-on failure; consider raising FileNotFoundError (or another explicit exception) when the file is absent so callers can stop early and return a clear error.
| log.error(f"whatsapp_bot.js bulunamadı: {bot_path}") | |
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | |
| else: | |
| ensure_npm_deps() | |
| msg = f"whatsapp_bot.js bulunamadı: {bot_path}" | |
| log.error(msg) | |
| log.error("Bot başlatılamıyor. Dosyayı proje dizinine kopyalayın.") | |
| raise FileNotFoundError(msg) | |
| ensure_npm_deps() |
| if not node_modules.exists(): | ||
| log.info("node_modules bulunamadı, 'npm install' çalıştırılıyor…") | ||
| try: | ||
| result = subprocess.run( | ||
| ['npm', 'install'], | ||
| cwd=str(APP_DIR), | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=300, | ||
| ) |
There was a problem hiding this comment.
ensure_npm_deps() can be reached from the threaded Flask server (/api/bot/start) and potentially by multiple gunicorn workers; as written, concurrent calls can launch multiple npm install processes against the same directory, risking corrupted/partial installs. Consider adding a process-level/thread-level lock (and ideally an inter-process file lock) around the install path and re-checking node_modules after acquiring the lock.
The bot process crashes immediately on startup because
node_modules/is never present in a fresh clone —npm installmust be run manually beforenode whatsapp_bot.jsis invocable.Changes
app.py—ensure_npm_deps(): New function that checks fornode_modules/and runsnpm installautomatically if missing. Handlesnpmnot found, non-zero exit (logs combined stdout+stderr), and 5-minute timeout.app.py—ensure_bot_file(): Now callsensure_npm_deps()whenwhatsapp_bot.jsis present, so dependencies are guaranteed installed beforeBotMonitor.start()launches the Node process..gitignore: Added to excludenode_modules/,.wwebjs_auth/,.wwebjs_cache/, Python bytecode, runtime databases, and.envfiles — none of which should be committed.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.