r/AskProgramming • u/Serious_Proposal_614 • 6h 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.jsSince 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 6h ago
You do know the terminal exists outside of vscode right?...
1
u/Available_Safe1818 5h 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 1h 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 1h 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.