Search code examples
javascriptnode.jscommand-prompt

.js runs in Visual Studio Terminal, but not from Command Prompt via Node


index.js contains console.log('hello World');

When I use node to run this in the terminal within visual studio code it works. (returns hello World) However when I attempt to run the file via node within the windows command prompt it fails.

Below is as it runs in the Visual Studio terminal:

PS C:\js-basics> node index.js
hello World
PS C:\js-basics>

This is the result from command prompt:

C:\Users\godbe>PS C:\js-basics> node index.js
'PS' is not recognized as an internal or external command,
operable program or batch file.

What is the proper syntax I would need to enter into the command prompt to run index.js (reads 'hello world' to console)?

I am expecting hello world to log to console.

I have tried variations such as:

C:\Users\godbe>C:\js-basics node <index>.js
The system cannot find the file specified.

C:\Users\godbe>C:\js-basics $node index.js
'C:\js-basics' is not recognized as an internal or external command,
operable program or batch file.

Solution

  • You're likely running a Powershell command in Command Prompt. Notice how 'PS' appears; that indicates that its a Powershell command.

    Try to open 'Command Prompt' and then execute your command (node idex.js) in the directory.


    Update:

    You need to execute the command in the correct directory (i.e. C:\js-basics) where the index.js resides

    To navigate to directory C:\js-basics, you need to first run the command cd C:\js-basics