grimaldodev
grimaldodev

Reputation: 320

Run node server with a bat file

I need to create a bat file to start node server, actually we do it manually but some people need extra help. Let me explain the process:

  1. Open CMD
  2. Go to the path: cd C://user/folder/server/
  3. Run the server: npm start

It is very simple but I would really like to automate the process to make it faster.

Upvotes: 19

Views: 93532

Answers (10)

Utlamo
Utlamo

Reputation: 1

I use other method: add to windows environment variables folder with global bat scripts (eg. C:/my_commands) next create in this folder file .bat where name is a command (eg. start_server.bat) Inside script write:

@echo off
start /B /WAIT /D C:\exemple\url\to\node\app\folder node index.js

Now you can call your server start from start_server command prompt anywhere if you need arguments use this:

start /B /WAIT /D C:\exemple\url\to\node\app\folder node index.js *%

if you need current (call) location in script use this:

start /B /WAIT /D C:\exemple\url\to\node\app\folder node index.js %cd%

This way you can start console node app from windows explorer :D

I know this is old thread but people looking for this all time.

Upvotes: 0

Diego Fortes
Diego Fortes

Reputation: 9790

I have two codes that I need to execute from a bat file.

  1. npm start from the frontend folder
  2. node index.js from the backend folder

Here is how I do it:

  1. I create a file named start.bat located in the root directory.
  2. Now the root directory has the frontend folder, the backend folder and the start.bat file.
  3. The code I'm using for the start.bat is the following:
@echo

start cmd.exe /k "node backend/index.js"

start cmd.exe /k "cd frontend && npm start"

It will open two different command prompt windows and execute both the node index.js code and the npm start. I hope it helps someone.

Upvotes: 0

Breakaway
Breakaway

Reputation: 41

Just make a bat file in the folder where your program is. Then type this in the bat file:

node (your program)

example: node server.js

Then, save the bat file and run it. It worked for me.

Upvotes: 4

Fisheiyy
Fisheiyy

Reputation: 65

If you are trying to start a discord bot of some sorts then try this :

// this is my path so if your is different change it and this needs to stay open for the bot to run
cd C:\salmon\salmon
node bot.js

Upvotes: 2

Anonymous
Anonymous

Reputation: 131

Is it just me or is the answer already in plain sight? The following has worked for me perfectly (with OP's path):

cd "C:/user/folder/server"
node index.js

Upvotes: 13

John Smit Conor
John Smit Conor

Reputation: 29

Alternative method, will be to install Bash environment for Windows and create file with name start.sh

#!/usr/bin/env bash

npm start

Or

#!/usr/bin/env bash

node <yourfilename>.js

Upvotes: 0

Jordi Piqueras
Jordi Piqueras

Reputation: 1

This not really works: "START /WAIT bitsadmin.exe /transfer "Downloading" http://nodejs.org/dist/v0.8.11/%NODE_EXEC% C:\node-v0.8.11-x86.msi"

I don't know why, but the rest should work:

@echo off

NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    echo This setup needs admin permissions. Please run this file as admin.
    pause
    exit
)

set NODE_VER=null
set NODE_EXEC=node-v0.8.11-x86.msi
set SETUP_DIR=%CD%
node -v >tmp.txt
set /p NODE_VER=<tmp.txt
del tmp.txt
IF %NODE_VER% NEQ null (
    echo INSTALLING node ...
    mkdir tmp
    IF NOT EXIST tmp/%NODE_EXEC% (
        echo Node setup file does not exist. Downloading ...
        cd ../bin
        START /WAIT bitsadmin.exe /transfer "Downloading" http://nodejs.org/dist/v0.8.11/%NODE_EXEC% C:\node-v0.8.11-x86.msi
        rem START /WAIT wget http://nodejs.org/dist/v0.8.11/%NODE_EXEC%
        move %NODE_EXEC% %SETUP_DIR%/tmp
    )
    cd %SETUP_DIR%/tmp
    START /WAIT %NODE_EXEC%
    cd %SETUP_DIR%
) ELSE (
    echo Node is already installed. Proceeding ...
)

Upvotes: 0

dahol
dahol

Reputation: 353

Know this post is old, but just throwing it out there: I just made a super simple .bat-file to start it:

cd C:\nodejs

"C:\MongoDB\bin\mongod.exe" --dbpath C:\MongoDB\bin\data\db

node server.js

Upvotes: 11

MarkNg
MarkNg

Reputation: 69

First you must add node.js install folder to path.
Next make a bat file named start.bat
Then, in the file, write

C:/user/folder/server/
npm start

Last click start.bat

Upvotes: 6

Reza Ebrahimi
Reza Ebrahimi

Reputation: 3689

You can start node.js server as following .bat script file by click on it:

@echo off
echo.

set NodePackagesPath=E:\Projects\OpenShift\Materials\Node.jsPackageManager // This is my path, you can edit them

set Path=%NodePackagesPath%\node_modules\.bin;%PATH%
set Path=%NodePackagesPath%;%PATH%

set NODE_PATH=%NodePackagesPath%\node_modules;%NODE_PATH%
set NODE_ENV=production

echo Environment variables are successfully added.
echo. 
echo. 
echo. 

node server.js

Upvotes: 7

Related Questions