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

  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

16 comments sorted by

4

u/UnexpectedSalami 4d ago

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

-5

u/Serious_Proposal_614 4d ago

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

6

u/Rscc10 4d ago

Yes, that's how servers work

1

u/dubidub_no 4d ago

No, it's not. A server process, or daemon, runs independent of a terminal.

0

u/lgastako 4d 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.

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.

  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 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/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"

2

u/dubidub_no 4d ago

A vibe coder would have figured this out with a prompt.

0

u/Serious_Proposal_614 3d ago

i am not a vibe coder bro or i could've just told ai to do this not post it in reddit

(no offense though - 😅)