Can't write to console

Hello, day 1 javascript noob here. I can’t get my javascript script code to run in my chrome console or using my visual studio codes terminal.

I’m trying to use visual studio code to write javascript. I’ve made a folder and inside the folder, I have two files index.html and index.js. I also have node installed.

index.html contents:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Hello World2</h1>
    <script>src="index.js"</script>
</body>
</html>

index.js contents:
console.log('hello');

I then right-click my HTML file and click open with live server as I have the addon installed. But when I check the console on chrome I can’t see anything there.

If I try and run the code using visual studio codes terminal, by typing $node index.js I get this error:

+ $node index.js
+       ~~~~~~~~
Unexpected token 'index.js' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

I’ve removed adblocker from chrome as well.

I am completely dumbfounded and have no idea how to fix this. Any help would be amazing.

<script src="index.js"></script>

It’s a typo, your closing > is just in the wrong place

Also, if it’s in the terminal you’re typing commands into, and that is literally what you’ve typed, then $node isn’t a command, the program is node, so it’s node ./index.js, assuming you’re executing in the folder “index.js” is in

Cheers, I can’t believe I missed that lol.

Yea did try “node index.js” before but got the same error but after I made that syntax change it works now.

Thanks a ton!

The error I was getting did say something wrong with the code in index.js so I should have picked up on that and looked at my syntax compared to the tutorial I was watching more.

Cheers
-Kye

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