[英]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. 我刚刚开始学习JavaScript,并且已经开始从事此基本练习。 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". 我已经写了一些基本的脚本,将“ Hello World”写到控制台,并且我试图在Visual Studio Code的集成终端中使用node.js执行文件“ index.js”,但是没有执行该脚本,它返回“ ReferenceError:索引未定义”。 If it helps, the contents of index.js are as follows: 如果有帮助,index.js的内容如下:
console.log('Hello World');
You are running node.js, and then typing index.js
at the REPL. 您正在运行node.js,然后在REPL上键入index.js
。
This causes node to try to interpret what you are typing as JavaScript. 这将导致节点尝试将您键入的内容解释为JavaScript。 It does not cause it to try to run the JS in the file called index.js
. 它不会导致它尝试在名为index.js
的文件中运行JS。
It tries to read the variable index
(which you haven't defined) and then access the .js
property on that object. 它尝试读取变量index
(您尚未定义),然后访问该对象的.js
属性。
You need to either: 您需要:
node index.js
通过输入node index.js
来指定要在启动Node.js时运行的JS require("./index")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.