简体   繁体   English

无法直接在系统上运行我的 JavaScript 代码

[英]Can't run my JavaScript code directly on system

I am unable to run this javascript code in the terminal even though the code is correct!即使代码正确,我也无法在终端中运行此 javascript 代码! I am using visual studio.我正在使用视觉工作室。 and it runs perfectly when I click on run code but doesn't run in terminal当我点击运行代码但没有在终端中运行时它运行良好

错误捕捉

You can't execute your JavaScript that way because your computer does not know how to.您无法以这种方式执行 JavaScript,因为您的计算机不知道如何执行。

(Computer) code can only get executed in a certain environment. (计算机)代码只能在特定环境中执行。 E. g.例如Java needs the JRE (Java Runtime Environment), and JavaScript needs its own environment, too. Java 需要 JRE(Java 运行时环境),JavaScript 也需要自己的环境。


One environment would be your webbrowser (what JavaScript was initially designed for).一种环境是您的网络浏览器(JavaScript 最初是为它设计的)。

So if I execute JavaScript here in the webbrowser, it works!因此,如果我在网络浏览器中执行 JavaScript,它会起作用! Why?为什么? Because it has a JavaScript engine (the environment JavaScript needs to be executed in) already built in.因为它已经内置了一个 JavaScript 引擎(需要在其中执行 JavaScript 的环境)。

 const sayHello = name => { return "hey there " + name + "!"; }; console.log(sayHello("John"));


If you want to execute JavaScript directly on your system like you tried, you will need to install such an evironment like your browser already has.如果你想像你尝试的那样直接在你的系统上执行 JavaScript,你需要像你的浏览器一样安装这样的环境。 Node.js would be such an option. Node.js就是这样一个选择。

After you have installed it on your system, you can run node your-file.js and you will see your expected output on the console.在您的系统上安装它后,您可以运行node your-file.js ,您将在控制台上看到预期的输出。

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

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