r/AskProgramming • u/Serious_Proposal_614 • 4d ago
Javascript How Can I Run My Node.js WhatsApp Bot in the Background Without Keeping VS Code Open?
I'm developing a WhatsApp moderation bot in JavaScript that automatically detects and moderates NSFW content, including text messages, GIFs, voice messages, and other media. For this, I'm using tools such as Whisper (Base), Tesseract OCR, and CLIP.
At the moment, whenever I want to start the bot, I have to open VS Code, navigate to the project directory, and run:
node index.js
Since index.js is the main file, this starts the bot. However, the problem is that the bot shuts down as soon as I close VS Code.
What I'd like to achieve is a way to keep the bot running in the background, independently of VS Code.
Ideally, my workflow would look something like this:
- I turn on my PC and open VS Code.
- I start the bot by running
node index.jsin the terminal. - Once the bot is running, I can close VS Code without terminating the bot.
- The bot continues running in the background without requiring VS Code to stay open.
- When I shut down my PC at night, the bot stops. The next day, when I turn my PC back on, I can start the process again—or have it launch automatically at startup.
I'd also like to know whether there's a way to keep the bot operational when my PC enters sleep mode.
Essentially, I'm looking for a reliable way to run my Node.js WhatsApp moderation bot as a background process or Windows startup service, rather than having to keep VS Code open all the time.
4
u/UnexpectedSalami 4d ago
You do know the terminal exists outside of vscode right?...
2
u/CheezitsLight 4d ago
Use nssm. It's official name is the non-sucking service manager and it does not suck you can do it from the command line with one command and it will install it as a service and run it whenever you turn your PC on.
1
u/Available_Safe1818 4d ago
You can set it up to run without a terminal, but how depends on your OS. Can you google 'windows startup service'?
1
u/Ok_Entrepreneur_8509 4d ago
The most useful thing you can do is learn how to package your app as a container. This is how the vast majority of software gets deployed in the cloud, so it is never too early to learn how to do it.
There are plenty of tutorials out there for learning the basics of Docker. Your Dockerfile is only going to be a few lines long.
This way you don't have to mess with systemd or launchd, or however windows does services.
1
u/PLBjt 4d ago
Sounds like you're on Windows, so the cleanest thing is probably PM2 plus a Task Scheduler entry that runs 'pm2 resurrect' at logon. Run it with pm2 start index.js, then pm2 save, and the bot lives in its own process instead of hanging off VS Code or a terminal. Check it by closing everything, logging out and back in, and seeing if the process is still listed. Heads up that sleep will pause the process and the WhatsApp socket usually drops, so you'd want reconnect handling or just disable sleep while plugged in. A real Windows service (NSSM or similar) survives logoff too, but it's fiddlier to debug when it won't start, so I'd begin with PM2.
1
u/AdhesivenessFew8441 4d ago
On Windows the easiest way is pm2. Run npm install -g pm2 once, then from the project folder run pm2 start index.js --name wa-bot and you can close the terminal and VS Code, the bot keeps running. pm2 logs wa-bot shows the output, pm2 restart wa-bot after you change the code, and it also restarts the bot by itself if it crashes. For starting it when Windows boots, run pm2 save, then create a task in Task Scheduler that runs pm2 resurrect at log on (the pm2 startup command does not support Windows). About sleep mode: no process keeps running while the PC sleeps, so either set the PC to never sleep while plugged in, or move the bot to a small VPS later. Keep in mind Whisper and CLIP need a good amount of RAM, so check that before picking a cheap server.
1
u/davidbalbino 3d ago
one gotcha nobody mentioned: if you're using whatsapp-web.js or Baileys, the session folder (.wwebjs_auth / auth_info) gets resolved from the current working directory. When pm2 or nssm starts the bot from somewhere else, it can't find the session and asks for the QR code again, or it just hangs waiting for a scan you can't see. So set the cwd explicitly (
pm2 start index.js --name wa-bot --cwd C:\path\to\bot, or AppDirectory in nssm), or use an absolute path in the auth strategy (new LocalAuth({ dataPath: path.join(__dirname, '.wwebjs_auth') })).about sleep: no, in sleep the process is frozen, nothing runs. If you want it 24/7 you have to disable sleep (
powercfg /change standby-timeout-ac 0) and turn off just the screen, or move it to a server. With Whisper + CLIP loaded tho, a small VPS gets tight on RAM pretty fast, so I think is better to keep it on your PC with sleep off for now.1
u/pLeThOrAx 3d ago
You could try http://nssm.cc/
1
u/Different_Pain5781 3d ago
Pm2 exists for a reason.
-1
u/ouroborus777 4d ago
Tell us you're a vibe coder without saying "vibe coder"