19

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.

Anderson
  • 138
  • 7
grimaldodev
  • 191
  • 1
  • 1
  • 5
  • 4
    You have your batch file right there. Just add those lines and save it as npmstart.bat or similar. – Matt Williamson Nov 26 '13 at 15:00
  • Just as an info, I hoped that there’s something like wamp for node like php , but the way nodejs worker is not like php .. so it -node,includes its server , so the best way is to ise .bat file . – mercury Jun 20 '21 at 01:36

10 Answers10

13

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
Anonymous
  • 131
  • 1
  • 2
11

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
dahol
  • 323
  • 3
  • 7
7

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
Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
  • can you run it if you dont have nodejs if not how can i also nodejs in it – Muhammad Umer Mar 11 '14 at 03:10
  • No, you can not, you must have node executable file, download it and add its path to environment variables, then you can use it anywhere by its name 'node', if you just change the above script paths to your paths, it works for you. – Reza Ebrahimi Apr 21 '14 at 17:01
  • is it possible to patch together bat and nodejs files together into one exe file. – Muhammad Umer Apr 22 '14 at 16:10
  • maybe! search on it, but its not matter, why do you want to do it? – Reza Ebrahimi Apr 22 '14 at 16:30
  • so i have one portable server all in one file that i can put in anywhere and just write local host and see the example for testing purposes. If there is node alternative i'd take it too. Like one exe file that opens a server inside that folder and default file is index.html and all other files can be accessed using their name. – Muhammad Umer Apr 22 '14 at 18:46
  • @MuhammadUmer: that's great! – Reza Ebrahimi Apr 22 '14 at 19:13
  • write now i have to copy and paste this folder of server that makes parent a server and all files in it are accessible. If i had exe file the step of going into the folder and double clicking won't be needed. – Muhammad Umer Apr 24 '14 at 03:47
  • @MuhammadUmer I know this is old but sharing with anyone who read this. This page http://www.f2ko.de/en/cmd.php at the bottom, 2 to last zip file is **webserver.exe** and you simply run `webserver.exe folder_path_to_web_files port_number` so you can do `webserver.exe web_files 8080` and it will make localhost load the `web_files` folder. It only serve static files like html, css, js, images, etc...and not PHP. Also port number can be any that you choose and is available – JasonDavis Apr 30 '17 at 20:56
  • @MuhammadUmer also the same website I linked to for the webserver has a program to convert .bat files into .exe programs which is nice in some cases – JasonDavis Apr 30 '17 at 20:59
6

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

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
MarkNg
  • 69
  • 1
  • 1
  • 1
    do not name your batch file 'start.bat' because there is already a command by the name of 'start'.. try something else.. e.g. mystart.bat or go.bat – joedotnot Sep 18 '18 at 00:31
4

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.

Breakaway
  • 41
  • 1
2

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
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
Fisheiyy
  • 65
  • 6
0

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 ...
)
0

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
0

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.

Diego Fortes
  • 8,830
  • 3
  • 32
  • 42
0

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.

Utlamo
  • 1