'Getting this error on running js using node : Uncaught SyntaxError: Unexpected identifier

The below code works fine in browser but when I try to run via node (in command prompt), it give error : Uncaught SyntaxError: Unexpected identifier

Please find the screenshot attached for an error.

//index.js

// This is my first JS code
console.log('Hello World');
//index.html 

<!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 World</h1>  
    <script src="index.js"></script> 
</body>
</html>

enter image description here



Solution 1:[1]

Found the problem. I started a node then ran below commands:

node
node index.js

All I had to do is just go to the directory and run this single command >> node index.js

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BAA