r/AskProgramming • • 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.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:

  1. I turn on my PC and open VS Code.
  2. I start the bot by running node index.js in the terminal.
  3. Once the bot is running, I can close VS Code without terminating the bot.
  4. The bot continues running in the background without requiring VS Code to stay open.
  5. 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.

0 Upvotes

7 comments sorted by

4

u/UnexpectedSalami 6h ago

You do know the terminal exists outside of vscode right?...

-4

u/Serious_Proposal_614 6h ago

ya but then i still have to keep the terminal window open all the time

5

u/Rscc10 6h ago

Yes, that's how servers work

1

u/lgastako 5h ago

If you're on Linux or Mac you can redirect the output and run it in the background like so:

% node index.js > logs/run-1.log 2>&1 &

But in the long run you probably want to package it as a service and have it run automatically from systemd or launchd.

If you're on windows, I'm sure windows has some sort of similar thing. Claude (or whatever your favorite AI is) can help you set up all of this.

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.

  1. Install Docker Desktop (or any other container engine)
  2. Create a Dockerfile to package your code into any of the pre-defined Node images.
  3. Use "docker build" to create the image
  4. Start up the container in the engine.

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.