Getting Started with JavaScript in VSCode

Trying to complete the exercise where you begin with installing node.js;

  • VSCode - this is the text editor we’ll be working with.
  • Node - is a runtime environment that will execute our JavaScript program.

Created a javascript file in VSC with following code; console.log(‘Hello, World!’);

Have saved it here; C:\Online Learning\CodeCademy\javascript\helloJavaScript

Can’t seem to find instructions on how to execute file/ navigate to file/ load in file in node.js or in terminal within VSC.

What am I missing here?

The MIME extension for JavaScript file names is, js.

path/hello.js

To run it, open the command line, set current working directory, and type and enter:

$ node hello.js

Sure, but how do I navigate to the path?
C:\Online Learning/CodeCademy/javascript/helloJavaScript/hello.js

Returns “Uncaught SyntaxError: Invalid or unexpected token”

C:/Online Learning/CodeCademy/javascript/helloJavaScript/hello.js

Returns “Uncaught SyntaxError: Invalid or unexpected token”

C:\Online Learning\CodeCademy\javascript\helloJavaScript\hello.js
Returns” “Uncaught SyntaxError: Invalid or unexpected token”

I realise it’s probably a pretty simple thing but it’s not really explained exactly correct syntax to type in to specify file path.

Kind regards -Jim

Does it give a line number in the error message?

In VSC there must be navigation tree on the left side? Locate your file in that tree and there might be an option to run or preview. Can’t say. myself.

As for the path, in the command line use ‘cd’ and enter the path you have above.

Well, not really because I guess there is no error in the code and it’s literally one line of code: 1 console.log(‘Hello, World!’);

Navigation tree? I mean not so much, it’s got the source folder with the single .js file in there…

Yeah I tried navigating in node.js using those bash commands the course covered briefly in earlier modules. It states “cd is not defined”, and …/ same error message.

Maybe I’m just not understanding what node.js does even!?

So, for example if I want to access a file in my c:// drive;

If it is located in a folder called “helloJavaScript” in “users”…

What do I specifically type into node.js terminal to access the file that is located at C://>users>helloJavaScript>hello.js

?

The place to set the path is in the command line. Node.js can be invoked from any directory. What’s important is to get the current working directory to be the one where your file is stored.

in the command line,

$ pwd

should tell you which directory you are currently in.

$ ls

should give you a list of contents. Is the hello.js file there? if so,

$ node hello.js

will work as expected. As for the token error,

Be sure those quotes are standard, not stylized.

console.log('Hello World!')

<img width=“851” height=“403” style=“width:8.8645in;height:4.1979in” id=“Picture_x0020_3” src=“//cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/codecademy/original/5X/8/1/5/e/815e2acd9f36313a8c669f75cff7a2e9a8f80743.jpeg” alt="Text

Description automatically generated">

Node.js is not the command line. In the Search bar next to the Start button, type,

cmd

and Enter. Now navigate to the folder where your file is,

It should default to,

C:\Users\yourusername>

which can be changed as follows:

cd helloJavaScript

Now your node command should work.

...> node hello.js

The Command App is actually quite outdated. Windows has PowerShell which is closer to bash and recognizes some of the bash commands.

Bottom line, invest yourself over time to learn both PowerShell (if you are a Windows user) and bash (whether linux user or not). VS Code might even have bash installed. Check your menus.

Another way to find out if Bash is present would be open Windows Explorer and navigate to a folder that contains a JS file. Right Click. Is ‘Git bash’ in the context menu? Of so, click it and voila. Now you can,

$ node myfile.js

But if VSCode is installing bash, it should have some way to open it within the IDE window. Check at the bottom as well as the top navigation.

Found this page…

How to integrate Git Bash with Visual Studio Code? - GeeksforGeeks

Oh, so what is the terminal for node.js? Is node.js just like a plugin for the Command Prompt? Yes, that worked in command Prompt.

Thank you so much for your assistance.

Node.js is a terminal for running Node commands. It is a shell within a shell. The outer shell, bash, invokes the node shell as the JS environment. We cannot run JS in bash (or PS or cmd). Node gives us the local support for ECMAScript which does not depend upon a browser.

Open your Chrome browser and Ctrl+Shif+J and you should have a JS console. It is possible to enter JS commands directly at the prompt.

 > console.log('Hello World!')
<- Hello World!
 >

That is not Node.js, but the V8 JavaScript engine of Chrome acting as the code environment. Node.js is a C based operating layer that exploits both V8 and C in a mixed way that makes JS run much faster than it can in a browser, alone. so much so it is a prominent environment on servers as a back end language interface. It lets us run JS on a server, which was unheard of before Node.js made the scene.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.