如何在 node.js 中执行 hello world javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15206849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:44:31  来源:igfitidea点击:

How to execute a hello world javascript file in node.js

javascriptnode.jscmd

提问by Joe Slater

I have hello_world.jsfile located on my computer at the following location -

我已经hello_world.js位于我的电脑上,在以下位置的文件-

C:\Users\Ankur\Downloads\NodeJS\working

It has the following code-

它有以下代码-

console.log("Hellow World");

I was reading a beginners tutorial hereand it says I should execute my javascript file through node.js. Now I have no idea what to do. How would I do this.

我在这里阅读初学者教程,它说我应该通过 node.js 执行我的 javascript 文件。现在我不知道该怎么办。我该怎么做。

When I do this ... enter image description here

当我这样做... 在此处输入图片说明

I get nothing. What should I be doing to run the hello world successfully. None of the tutorials seem to be showing what needs to be done.

我什么也得不到。我应该怎么做才能成功运行 hello world。似乎没有一个教程显示需要做什么。

I am very new to this so it is probably something that is very obvious to you guys.

我对此很陌生,所以这对你们来说可能很明显。

回答by Musa

Use Node.js command prompt, then type in node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

使用Node.js command prompt,然后输入node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

回答by Plato

looks like you are in a node console already. you typed nodewhich correctly started node (so your path is fine). Now you are telling node to interpret

看起来您已经在节点控制台中了。你输入了node哪个正确启动的节点(所以你的路径很好)。现在你告诉节点解释

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

as javascript which it doesn't like.

作为它不喜欢的 javascript。

You should be able to type console.log('hello world');here and see it appear.

您应该能够在console.log('hello world');此处键入并看到它出现。

To run your file, quit out of the node interpreter (i think control-X, maybe control-C), and at your C:> prompt, THEN type

要运行您的文件,请退出节点解释器(我认为是 control-X,也可能是 control-C),然后在您的 C:> 提示符下输入 THEN

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

Alternately,

交替,

cd C:\Users\Ankur\Downloads\NodeJS\working\
node hello_world.js

回答by Billy Moon

You have entered the node console, by typing nodeinto a command prompt, and then tried to execute node from within itself. Just type node c:\etc\...\from the command prompt, not the node shell.

您已通过node在命令提示符中键入来进入节点控制台,然后尝试从其内部执行节点。只需node c:\etc\...\从命令提示符键入,而不是节点 shell。

Press: [Start]->[Run]->[c][m][d]

按:[开始]->[运行]->[c][m][d]

And enter command: node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

并输入命令: node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

Alternatively, use sublime editor, and install a node js build system, and just open the file you want to run, and press the build shortcut, getting the output in the console.

或者,使用 sublime 编辑器,并安装一个 node js 构建系统,只需打开要运行的文件,然后按构建快捷方式,即可在控制台中获得输出。

回答by sarathmojo

  1. Open CMD.
  2. Type "cd C:\Users\Ankur\Downloads\NodeJS\working" and hit Enter
  3. Type "node hello_world.js" and hit Enter
  1. 打开 CMD。
  2. 输入“cd C:\Users\Ankur\Downloads\NodeJS\working”,然后按 Enter
  3. 输入“node hello_world.js”并按回车

This should work out !! IMPORTANT: Dont type node before the point number 3.

这应该解决!重要提示:不要在点号 3 之前输入节点。

回答by Ahmed Atia

Follow these three easy steps:

请遵循以下三个简单步骤:

  • Go to your working directory

    C:\Users\Ankur\Downloads\NodeJS\working 
    
  • Open cmd there and type

    node hello_world
    
  • Press Enter

  • 转到您的工作目录

    C:\Users\Ankur\Downloads\NodeJS\working 
    
  • 在那里打开cmd并输入

    node hello_world
    
  • Enter

回答by Oliver Kalima Tembo

Alternative. Open Nodejs "The thing with a green icon". Copy the code that you would like to be executed from the file that you have

选择。打开 Nodejs“带有绿色图标的东西”。从您拥有的文件中复制您想要执行的代码

paste it in nodejs then press enter

将其粘贴到 nodejs 中,然后按 Enter

nodejs understands js code so it will execute what you have written and output hello world undefined

nodejs 理解 js 代码,因此它会执行您编写的内容并输出 hello world undefined

What you have descripbed opens the file that you have and then executes it.

您所描述的内容会打开您拥有的文件,然后执行它。

回答by user568109

Open Windows cmd and type

打开 Windows cmd 并键入

node C:\Users\Ankur\Downloads\NodeJS\working\hello_world.js

You should use node from system interpreter (bash/DOS cmd), what you are running is C:\Program Files\nodejs\node.exe without any arguments.

您应该使用系统解释器(bash/DOS cmd)中的节点,您正在运行的是 C:\Program Files\nodejs\node.exe 不带任何参数。