Skip to content

Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js'#4

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-whatsapp-web-module-error
Open

Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js'#4
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-whatsapp-web-module-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 11, 2026

The bot process crashes immediately on startup because node_modules/ is never present in a fresh clone — npm install must be run manually before node whatsapp_bot.js is invocable.

Changes

  • app.pyensure_npm_deps(): New function that checks for node_modules/ and runs npm install automatically if missing. Handles npm not found, non-zero exit (logs combined stdout+stderr), and 5-minute timeout.

  • app.pyensure_bot_file(): Now calls ensure_npm_deps() when whatsapp_bot.js is present, so dependencies are guaranteed installed before BotMonitor.start() launches the Node process.

  • .gitignore: Added to exclude node_modules/, .wwebjs_auth/, .wwebjs_cache/, Python bytecode, runtime databases, and .env files — none of which should be committed.

# Before starting the bot, ensure npm deps are present
def ensure_npm_deps() -> None:
    node_modules = APP_DIR / "node_modules"
    if (APP_DIR / "package.json").exists() and not node_modules.exists():
        log.info("node_modules bulunamadı, 'npm install' çalıştırılıyor…")
        result = subprocess.run(['npm', 'install'], cwd=str(APP_DIR), ...)
        # logs success or combined stdout+stderr on failure

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…b.js' error

Co-authored-by: eeea2222 <209839587+eeea2222@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error on missing whatsapp-web.js module Auto-install npm dependencies on startup to fix Cannot find module 'whatsapp-web.js' Mar 11, 2026
@eeea2222
Copy link
Copy Markdown
Owner

ok

@eeea2222 eeea2222 marked this pull request as ready for review March 11, 2026 18:26
Copilot AI review requested due to automatic review settings March 11, 2026 18:26
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-run npm install when node_modules/ is missing.
  • Updated ensure_bot_file() to invoke ensure_npm_deps() when whatsapp_bot.js exists.
  • Introduced a repository .gitignore to 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.

Comment thread app.py
Comment on lines +3645 to +3653
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ı.")
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread app.py
Comment on lines 3660 to +3663
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()
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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()

Copilot uses AI. Check for mistakes.
Comment thread app.py
Comment on lines +3635 to +3644
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,
)
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants