如何在Windows,Mac或Linux上安装Node.js

In this tutorial, I’ll be teaching you how you can install Node.js on windows, mac or linux in under 15 minutes.

在本教程中,我将教您如何在15分钟内在Windows,Mac或Linux上安装Node.js。

Node is an open-source runtime environment for javascript. It is a cross-platform environment providing its support for Mac, Windows and Linux. It runs Chrome’s V8 javascript engine, outside the browser, and because of this, it is very powerful.

Node是javascript的开源运行时环境。 它是一个跨平台环境,提供对Mac,Windows和Linux的支持。 它在浏览器外部运行Chrome的V8 javascript引擎,因此它非常强大。

It runs in a single process, without creating a new thread for every request. Node JS

它在单个进程中运行,无需为每个请求创建新线程。 节点JS

节点应用程序外观如何? (How Does a Node App Look?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const http = require ( 'http' ) ;
const hostname = '127.0.0.1' ;
const port = 3000 ;
const server = http . createServer ( ( req , res ) = > {
   res . statusCode = 200 ;
   res . setHeader ( 'Content-Type' , 'text/plain' ) ;
   res . end ( 'Hello World' ) ;
} ) ;
server . listen ( port , hostname , ( ) = > {
   console . log ( ` Server running at http : //${hostname}:${port}/`);
} ) ;

如何安装Node.js? (How to Install Node.js?)

These tools are required for developing a Node.js application on any platform.

在任何平台上开发Node.js应用程序都需要这些工具。

  • Node JS

    节点JS

  • Node Package Manager or NPM*

    节点程序包管理器或NPM *
  • A text editor or an IDE

    文本编辑器或IDE

*NPM gets installed automatically when you install Node on your system.

*在系统上安装Node时,会自动安装NPM。

在Windows和MAC上安装 (Installation on Windows and MAC)

To download node, you just need to visit the node’s official website and download the LTS version of node.

要下载节点,您只需要访问该节点的官方网站并下载该节点的LTS版本。

Once, downloaded, you’ll see an installer similar to this.

下载完成后,您将看到与此类似的安装程序。

如何在Windows,Mac或Linux上安装Node.js

Hit install it as you install any other application on your Windows or Mac. Enter your system password if prompted.

在Windows或Mac上安装任何其他应用程序时,请点击安装。 如果出现提示,请输入系统密码。

如何在Windows,Mac或Linux上安装Node.js

And once done, the installer will prompt you a success message. You can now go ahead and delete the installer.

完成后,安装程序将提示您成功消息。 现在,您可以继续并删除安装程序。

如何在Windows,Mac或Linux上安装Node.js

在Linux上安装 (Installation on Linux)

To install Node on your Ubuntu machine, run the following command:

要在Ubuntu计算机上安装Node,请运行以下命令:

Once you download Node, the NPM or Node Package Manager automatically gets installed as it also ships with the node.

下载节点后,将自动安装NPM节点软件包管理器,因为它也随节点一起提供。

安装文本编辑器 (Installing the Text Editor)

The next thing is to install a text editor which will help you in writing code. The one which I personally use is Visual Studio Code by Microsoft.

接下来是安装一个文本编辑器,它将帮助您编写代码。 我个人使用的是Microsoft的 Visual Studio Code

You can download it from here: https://code.visualstudio.com/

您可以从这里下载: https : //code.visualstudio.com/

Once you are done with the text editor, the next thing which you can do is to check whether the path of the Node is correct or not.

使用文本编辑器完成操作后,下一步是检查Node的路径是否正确。

测试Nodejs和NPM安装 (Testing Nodejs and NPM installation)

You can check your node installation just by running a small node command which will show you the version installed. Just open your cmd or PowerShell and paste the below command. This will show the version of node installed on your computer.

您可以通过运行一个小的node命令来检查您的节点安装,该命令将向您显示安装的版本。 只需打开您的cmd或PowerShell,然后粘贴以下命令。 这将显示您计算机上安装的节点的版本。

1
2
node -v
v12.16.0
1
2
node - v
v12 . 16.0

Similarly, you can check the version of npm installed on your computer. Just paste the below node command to check the version.

同样,您可以检查计算机上安装的npm的版本。 只需粘贴以下node命令以检查版本。

1
2
npm -v
v6.13.4
1
2
npm - v
v6 . 13.4

运行您的第一个应用程序 (Running Your First App)

Here you are all set up, let’s try to run a small node server. Open VS Code and create a new file and save it as app.js

在这里,您已经完成了所有设置,让我们尝试运行小型节点服务器。 打开VS Code并创建一个新文件,并将其另存为app.js

Now paste the following code.

现在粘贴以下代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const http = require ( 'http' ) ;
const hostname = '127.0.0.1' ;
const port = 3000 ;
const server = http . createServer ( ( req , res ) = > {
   res . statusCode = 200 ;
   res . setHeader ( 'Content-Type' , 'text/plain' ) ;
   res . end ( 'Hello World' ) ;
} ) ;
server . listen ( port , hostname , ( ) = > {
   console . log ( ` Server running at http : //${hostname}:${port}/`);
} ) ;

Now save the code and hit Ctrl + ~

现在保存代码,然后Ctrl + ~

This will open an integrated terminal of vs code in the same directory and from here you can directly run your js code.

这将在同一目录中打开vs代码的集成终端,从这里您可以直接运行js代码。

Simply run the following command and hit enter.

只需运行以下命令,然后按Enter。

1
2
node app.js
Server running at http://127.0.0.1:3000/
1
2
node app . js
Server running at http : //127.0.0.1:3000/

You can now visit http://127.0.0.1:3000/ to check your node app running.

您现在可以访问http://127.0.0.1:3000/来检查节点应用程序的运行情况。

Well, this was all about setting up your system for js development. If you stuck into any kind of error, don’t forget to google it and try to debug it on your own.

好了,这就是为js开发设置系统。 如果您遇到任何类型的错误,请别忘了用Google搜索并尝试自行调试。

This will teach you how to debug on your own as someone might have faced a similar problem earlier.

这将教您如何自行调试,因为之前可能有人遇到过类似的问题。

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you.

如果您仍然找不到解决问题的方法,可以在下面的评论部分中提出疑问,我们将尽快与您联系。

翻译自: https://www.thecrazyprogrammer.com/2020/04/how-to-install-node-js.html