-1

I'm trying to figure out node.js and nothing is working. I can run code using node.js via writing it in the terminal, but I want to run an external js file.

Right now, I have my node.js installed here "C:\Program Files\nodejs\node.exe"

I then made a js file and wrote console.log("Hello World"); and saved it in the same location and called it test.js.

I then opened up a command prompt and tried everything to try run it but nothing works. The PowerPoint my teacher gave to me says to do "node test.js" and it should output Hello World.

When I try it, it says error cannot find test.js, so I wrote the full file path and it says, c:\program is undefined or something. Can someone please tell me what to write in the command prompt so I can output Hello World via the test.js file I've got saved in the same location as node.exe?

I am using windows if that helps.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Will
  • 348
  • 1
  • 2
  • 12
  • I think you need to add the path to node.js in your environment virables. – Rahul Desai Jan 26 '17 at 18:58
  • 2
    1. The full path to `node.exe` needs to [be in your PATH](http://stackoverflow.com/questions/4822400/register-an-exe-so-you-can-run-it-from-any-command-line-in-windows) to use the command `node`; 2. you need to use quotation marks around paths with spaces in them; the terminal will do this for you automatically if you use `Tab` to complete paths – apsillers Jan 26 '17 at 18:58
  • @apsillers, i just tried to write "c:\program files\nodejs\node.exe test.js and its saying "c:\program is not recognized as an internal or external command – Will Jan 26 '17 at 19:01
  • try "c:\program\ files\nodejs\node.exe test.js" – Aᴍɪʀ Jan 26 '17 at 19:03
  • Have you asked your teacher? Not to be rude, but there is a lack of understanding how simple computer concepts work in your question. Your teacher is going to be a better resource than StackOverflow will be at your point of education. – Qix - MONICA WAS MISTREATED Jan 26 '17 at 21:35

1 Answers1

1

You need to put test.js your current directory. If you just open CMD, your current directory defaults to your user directory. Instead of putting your js file next to the node.exe, you have to put it in the directory your cmd is in.

Example: create test.js in C:\Users\yourname\Documents\test.js then open CMD and cd C:\Users\yourname\Documents\ to set your CMD's current directory to your documents folder. From there, node test.js will run the test.js file you have in C:\Users\yourname\Documents\

pfg
  • 2,348
  • 1
  • 16
  • 37