如何在Windows的Node.js中运行hello.js文件?

本文翻译自:How to run a hello.js file in Node.js on windows?

I am trying to run a hello world program written in javascript in a separate file named hello.js 我正在尝试在一个名为hello.js的单独文件中运行用javascript编写的hello world程序

Currently running windows version of node.js. 当前正在运行Windows版本的node.js。

The code runs perfectly in console window but how do I reference the path in windows environment . 该代码可以在控制台窗口中完美运行,但是如何在Windows环境中引用该路径

C:\abc\zyx\hello.js

in Unix I guess it is showing $ node hello.js 在Unix中,我猜它显示的是$ node hello.js

I'm absolutely new to Node.js Please correct me if I am doing something wrong. 我绝对不是Node.js的新手,如果我做错了什么,请纠正我。

I tried 我试过了

> node C:\\abc\\zyx\\hello.js ----didn't work > node C:\\abc\\zyx\\hello.js ----无法正常工作

> C:\\abc\\zyx\\hello.js --didn't work > C:\\abc\\zyx\\hello.js zyx > C:\\abc\\zyx\\hello.js没用

UPDATE1: UPDATE1:

Added node.exe to the folder where hello.js file is sitting. 将node.exe添加到了hello.js文件所在的文件夹中。
Added path point to the folder c:\\abc\\zyx\\ and I get an error that says 添加了指向文件夹c:\\ abc \\ zyx \\的路径,但出现错误提示

ReferenceError: hello is not defined ReferenceError:您好未定义

see contents of hello.js 查看hello.js的内容

setTimeout(function() {
console.log('World!');
}, 2000);
console.log('Hello');

UPDATE 2: 更新2:

So far I have tried all these version and none of them seems to work . 到目前为止,我已经尝试了所有这些版本, 但似乎都没有用 May be I am doing something completely wrong. 可能是我做错了什么。

>node hello.js
>$ node hello.js
>node.exe hello.js
>node /hello.js
>node \hello.js
> \node \hello.js
> /node /hello.js
> C:\abc\xyz\node.exe C:\abc\xyz\hello.js
> C:\abc\xyz\node.exe C:/abc/xyz/hello.js
> hello.js
> /hello.js
> \hello.js
>node hello

Refer to my file structure 参考我的文件结构

RESOLVED: Instead of running node.exe, try running in command prompt with the following option and it worked. 已解决:而不是运行node.exe,请尝试使用以下选项在命令提示符下运行,并且该命令有效。

c:\>node c:\abc\hello.js
Hello
World! (after 2 secs)

#1楼

参考:https://stackoom.com/question/SGoa/如何在Windows的Node-js中运行hello-js文件


#2楼

I installed node for windows. 我为Windows安装了节点。 There is a node.js command prompt when I search for node.js in windows 7 start menu If you run this special command prompt, you can node anything in any location without setting up the path or copy node.exe everywhere. 当我在Windows 7开始菜单中搜索node.js时,会有一个node.js命令提示符。如果运行此特殊命令提示符,则可以在任何位置结点任何节点,而无需设置路径或将node.exe复制到各处。


#3楼

c:\> node.exe %CD%\hello.js

%CD%在DOS下捕获当前目录


#4楼

Install the MSI file: Go to the installed directory C:\\Program Files\\nodejs from command prompt n 安装MSI文件:从命令提示符n转到安装目录C:\\Program Files\\nodejs

C:\\>cd C:\\Program Files\\nodejs enter..

node helloworld.js

output: 输出:

Hello World


#5楼

another simple way 另一种简单的方法

  1. download nodejs to your system 将nodejs下载到您的系统
  2. open a notepad write js command "console.log('Hello World');" 打开一个记事本写js命令“ console.log('Hello World');”
  3. save the file as hello.js preferably same location as nodejs 将文件另存为hello.js,最好与nodejs位于同一位置
  4. open command prompt navigate to the location where the nodejs is located 打开命令提示符,导航到nodejs所在的位置
    c:\\program files\\nodejs
  5. and run the command from the location like c:\\program files\\nodejs>node hello.js 然后从c:\\program files\\nodejs>node hello.js这样的位置运行命令
  6. in case the js file in another location give the path of file c:\\program files\\nodejs>node path\\hello.js 如果其他位置的js文件给出了文件c:\\program files\\nodejs>node path\\hello.js

#6楼

For all stuck on how to start! 对于所有卡住了如何开始!

https://github.com/sethvincent/javascripting https://github.com/sethvincent/javascripting

Copy here incase link dies: 如果链接链接死亡,请在此处复制:

  1. Open node.js command prompt 打开node.js命令提示符
  2. Make directory called javascripting by typing "mkdir javascripting" 通过键入“ mkdir javascripting”来创建名为javascripting的目录
  3. Change directory into the javascripting folder by typing "cd javascripting" 通过输入“ cd javascripting”将目录更改为javascripting文件夹
  4. Create a file named introduction.js by typing "touch introduction.js" OR FOR WINDOWS: "NUL > introduction.js" 通过键入“ touch Introduction.js”来创建一个名为Introduction.js的文件,或者对于WINDOWS:“ NUL> Introduction.js”
  5. Open the file and type some javascript eg "Console.log('hello');" 打开文件并键入一些javascript,例如“ Console.log('hello');”。
  6. Save the file and check it runs by typing "javascripting verify introduction.js" 保存该文件并通过键入“ javascripting verify Introduction.js”检查其运行

你可能感兴趣的:(如何在Windows的Node.js中运行hello.js文件?)