Kim
Kim

Reputation: 3

Opening JavaScript code with node.js in Visual Studio Code's integrated terminal

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');

Upvotes: 0

Views: 1778

Answers (1)

Quentin
Quentin

Reputation: 943240

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")

Upvotes: 1

Related Questions