简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM