0

I'm just starting to learn JavaScript, and am already stuck with this basic exercise. I've written some basic script that writes "Hello World" to the console, and I'm trying to use node.js to execute the file "index.js" in Visual Studio Code's integrated terminal, but instead of executing the script, it returns "ReferenceError: index is not defined". If it helps, the contents of index.js are as follows:

console.log('Hello World');
3
  • Where is index.js located in your files, and where are you when you try to execute the script? (ie, what does pwd or the node equivalent give you, whatever that is?) Commented Jun 1, 2018 at 22:14
  • what command are you using to execute your index.js? Also, which terminal shell is being launched by vscode (cmd, node, bash, powershell, etc.)? Commented Jun 1, 2018 at 22:16
  • I'm not sure how to tell which terminal shell is being used, where can I find that out? edit Never mind, I see it states it right at the top, Windows Powershell. Commented Jun 1, 2018 at 22:31

1 Answer 1

1

You are running node.js, and then typing index.js at the REPL.

This causes node to try to interpret what you are typing as JavaScript. It does not cause it to try to run the JS in the file called index.js.

It tries to read the variable index (which you haven't defined) and then access the .js property on that object.


You need to either:

  • Specify the JS to run at the time you start Node.js by typing node index.js
  • require("./index")
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know what REPL is.
But the rest of it worked! I just had to kill the terminal to restart with node index.js. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.