简体   繁体   English

在Visual Studio Code的集成终端中使用node.js打开JavaScript代码

[英]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: 您需要:

  • Specify the JS to run at the time you start Node.js by typing node index.js 通过输入node index.js来指定要在启动Node.js时运行的JS
  • require("./index")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Visual Studio Code 和 Node.js 调试/学习 javascript - debugging/learning javascript using Visual Studio Code and Node.js 当我尝试在 Visual Studio Code 的终端中使用 Node.js 时,我收到“文档未定义”错误消息 - When I try to use Node.js in Visual Studio Code’s terminal, I get “document is not defined” error messages 为什么 Node.js 无法在终端中运行我的 JavaScript 代码? - Why Node.js cannot run my JavaScript code in the terminal? 以REPL方式直接从Visual Studio执行javascript(node.js)代码 - execute javascript (node.js) code right from Visual Studio in REPL manner Visual Studio Code 中调试控制台中的 Node.js readline - Node.js readline in debug-console in Visual Studio Code 使用Visual Studio Code在Node.JS中定义类型 - Defining types in Node.JS using Visual Studio Code Node.js 错误 Visual Studio 代码中未找到模块错误 - Node.js error Module not found error in visual studio code Visual Studio 代码断点在使用 TypeScript 的 Node.js 上不起作用 - Visual Studio code breakpoint not working on Node.js using TypeScript Visual Studio Code Node.js 创建模块 - Visual Studio Code Node.js Create Module 如何在不转译代码的情况下在 Visual Studio Code 中使用 ES6 模块调试 Node.JS? - How to debug Node.JS with ES6 modules in Visual Studio Code without transpiling the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM