[windows] How to run a hello.js file in Node.js on windows?

I am trying to run a hello world program written in javascript in a separate file named hello.js

Currently running windows version of node.js.

The code runs perfectly in console window but how do I reference the path in windows environment.

C:\abc\zyx\hello.js

in Unix I guess it is showing $ node hello.js

I'm absolutely new to Node.js Please correct me if I am doing something wrong.

I tried

> node C:\abc\zyx\hello.js ----didn't work

> C:\abc\zyx\hello.js --didn't work

UPDATE1:

Added node.exe to the folder where hello.js file is sitting.
Added path point to the folder c:\abc\zyx\ and I get an error that says

ReferenceError: hello is not defined

see contents of hello.js

setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');

UPDATE 2:

So far I have tried all these version and none of them seems to work. May be I am doing something completely wrong.

>node hello.js
>$ node hello.js
>node.exe hello.js
>node /hello.js
>node \hello.js
> \node \hello.js
> /node /hello.js
> C:\abc\xyz\node.exe C:\abc\xyz\hello.js
> C:\abc\xyz\node.exe C:/abc/xyz/hello.js
> hello.js
> /hello.js
> \hello.js
>node hello

Refer to my file structure

.
+-- hello.js
+-- node.exe
+-- paths.txt

RESOLVED: Instead of running node.exe, try running in command prompt with the following option and it worked.

c:\>node c:\abc\hello.js
Hello
World! (after 2 secs)

This question is related to windows node.js

The answer is


another simple way

  1. download nodejs to your system
  2. open a notepad write js command "console.log('Hello World');"
  3. save the file as hello.js preferably same location as nodejs
  4. open command prompt navigate to the location where the nodejs is located
    c:\program files\nodejs
  5. and run the command from the location like c:\program files\nodejs>node hello.js
  6. in case the js file in another location give the path of file c:\program files\nodejs>node path\hello.js

Go to cmd and type: node "C:\Path\To\File\Sample.js"


You need to make sure that node is in your PATH. To set up your path, this out.

Make sure that the directory that has node.exe is in your PATH. Then you should be able to run node path_to_js_file.js.

For a good "Hello World" example, check out: http://howtonode.org/hello-node


Windows/CMD does not know where the node file is located. You can manually type out:

path=%path%;"c:\Program Files\nodejs"

each time you open a new cmd.exe prompte

OR (in Windows 10),

  1. right click on This PC -> properties.
  2. Click on Advanced system settings -> Environment Variables (bottom right).
  3. Select Path and click Edit.
  4. Click new and enter C:\Program Files\nodejs.
  5. Reboot and you should be able to run node from any directory.

I installed node for windows. There is a node.js command prompt when I search for node.js in windows 7 start menu If you run this special command prompt, you can node anything in any location without setting up the path or copy node.exe everywhere.


type node js command prompt in start screen. and use it. OR set PATH of node in environment variable.


c:\> node.exe %CD%\hello.js

%CD% captures the current directory under DOS


For all stuck on how to start!

https://github.com/sethvincent/javascripting

Copy here incase link dies:

  1. Open node.js command prompt
  2. Make directory called javascripting by typing "mkdir javascripting"
  3. Change directory into the javascripting folder by typing "cd javascripting"
  4. Create a file named introduction.js by typing "touch introduction.js" OR FOR WINDOWS: "NUL > introduction.js"
  5. Open the file and type some javascript e.g. "Console.log('hello');"
  6. Save the file and check it runs by typing "javascripting verify introduction.js"

Just change file association of .js file to node.exe and you can run directly from explorer.

1) Right click on the file -> Select "Open with" -> Select "Choose another program"
2) Check box "Always use this app to open .js file"
3) Click "More apps" -> "Look for another app in PC"
4) Navigate to node.js installation directory.(Default C:\Program Files\nodejs\node.exe"
5) Click "Open" and you can just see cmd flashing

Now you will be able to run any .js files directly just by double clicking.

Note: Adding below code to the end of js file will be useful as it will keep the console open for a keystroke.

console.log('Press any key to exit');

process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));

I had such problem for windows. And I decided it so: startApp.cmd:

@set JAVA_HOME=C:\jdk160_24
@set PATH=%JAVA_HOME%/bin;%PATH%
@cd /D C:\app\

@start /b C:\WINDOWS\system32\cscript.exe 
C:\app\start.js

and saved it cmd file in dir C:\scripts next file is runApp.bat:

C:\scripts\startApp.cmd

WinXp: I have created a .bat file

node c:\path\to\file\my_program.js

That just run my_program.bat from Explorer or in cmd window


Install the MSI file: Go to the installed directory C:\Program Files\nodejs from command prompt n

C:\>cd C:\Program Files\nodejs enter..

node helloworld.js

output:

Hello World


The problem was that you opened the Node.js repl while everyone automatically assumed you were in the command prompt. For what it's worth you can run a javascript file from the repl with the .load command. For example:

.load c:/users/username/documents/script.js

The same command can also be used in the command prompt if you first start node inside the command prompt by entering node with no arguments (assuming node is in PATH).

I find it fascinating that 1)everyone assumed you were in the command prompt rather than repl, 2)no one seems to know about .load, and 3)this has 273 upvotes, proving that a lot of other node.js beginners are similarly confused.


Step For Windows

  1. press the ctrl + r.then type cmd and hit enter.
  2. now command prompt will be open.

  3. after the type cd filepath of file. ex(cd C:\Users\user\Desktop\ ) then hit the enter.

  4. please check if npm installed or not using this command node -v. then if you installed will get node version.
  5. type the command on command prompt like this node filename.js . example(node app.js)

C:\Users\user\Desktop>node app.js

All you have to do is right click the .js file on Windows and press "Open with Command Prompt" OR Open cmd, copy the path to the folder containing your script, and run the command "cd [paste text here]". Then do "node example.js"


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`