当前位置: 首页 > 工具软件 > Node.js Fast > 使用案例 >

如何在Windows上安装Node.js和npm

程项禹
2023-12-01

Installing Node.js and npm on Windows is very straightforward.

在Windows上安装Node.js和npm非常简单。

First, download the Windows installer from the Node.js website. You will have the choice between the LTS (Long Term Support) or Current version.

首先,从Node.js网站下载Windows安装程序。 您可以在LTS (长期支持)或当前版本之间进行选择。

  • The Current version receives the latest features and updates more rapidly

    当前版本接收最新功能并更新更快

  • The LTS version foregos feature changes to improve stability, but receives patches such as bug fixes and security updates

    LTS版本的foregos功能进行了更改,以提高稳定性,但会收到一些补丁,例如错误修复和安全更新。

Once you have selected a version meets your needs, run the installer. Follow the prompts to select an install path and ensure the npm package manager feature is included along with the Node.js runtime. This should be the default configuration.

选择满足您需求的版本后,运行安装程序。 按照提示选择安装路径,并确保npm软件包管理器功能随Node.js运行时一起提供。 这应该是默认配置。

Restart your computer after the installation is complete.

安装完成后,重新启动计算机。

If you installed under the default configuration, Node.js should now be added to your PATH. Run command prompt or powershell and input the following to test it out:

如果您是在默认配置下安装的,则应该将Node.js添加到PATH中。 运行命令提示符或powershell并输入以下内容进行测试:

> node -v

The console should respond with a version string. Repeat the process for npm:

控制台应以版本字符串响应。 对npm重复此过程:

> npm -v

If both commands work, your installation was a success, and you can start using Node.js!

如果两个命令都起作用,则说明安装成功,并且可以开始使用Node.js!

有关Node.js的更多信息 (More info on Node.js)

According to its GitHub repository, Node.js is:

根据其GitHub存储库 ,Node.js为:

Node.js is an open-source, cross-platform, JavaScript runtime environment. It executes JavaScript code outside of a browser. For more information on using Node.js, see the Node.js Website.

Node.js是一个开源,跨平台JavaScript运行时环境。 它在浏览器外部执行JavaScript代码。 有关使用Node.js的更多信息,请参见Node.js网站

Node.js事实细分: (A breakdown of Node.js facts:)

  • Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.

    Node.js是基于Chrome的V8 JavaScript引擎构建JavaScript运行时。

    Every browser has a JavaSript engine built in it to process JavaScript files contained in websites. Google Chrome uses the V8 engine, which is built using C++. Node.js also uses this super-fast engine to interpret JavaScript files.

    每个浏览器都内置有JavaSript引擎来处理网站中包含JavaScript文件。 Google Chrome使用V8引擎,该引擎是使用C ++构建的。 Node.js还使用此超快速引擎来解释JavaScript文件。

  • Node.js uses an event-driven model.

    Node.js使用事件驱动模型。

    This means that Node.js waits for certain events to take place. It then acts on those events. Events can be anything from a click to a HTTP request. We can also declare our own custom events and make Node.js listen for those events.

    这意味着Node.js等待某些事件发生。 然后,它对那些事件起作用。 事件可以是任何事件,从单击到HTTP请求。 我们还可以声明我们自己的自定义事件,并使Node.js监听那些事件。

  • Node.js uses a non-blocking I/O model.

    Node.js使用非阻塞I / O模型。

    We know that I/O tasks take much longer than processing tasks. Node.js uses callback functions to handle such requests.

    我们知道I / O任务比处理任务要花费更长的时间。 Node.js使用回调函数来处理此类请求。

Let us assume that a particular I/O task takes 5 seconds to execute, and that we want to perform this I/O twice in our code.

让我们假设一个特定的I / O任务需要5秒钟来执行,并且我们想在代码中执行两次该I / O。

Python

Python

import time

def my_io_task():
  time.sleep(5)
  print("done")

my_io_task()
my_io_task()

Node.js

Node.js

function my_io_task() {
    setTimeout(function() {
      console.log('done');
    }, 5000);
}

my_io_task();
my_io_task();

Both look similar, but the time taken to execute are different. The Python code takes 10 seconds to execute while the Node.js code takes only 5 seconds.

两者看起来相似,但执行时间不同。 Python代码执行需要10秒,而Node.js代码仅需要5秒。

Node.js takes less time because of its non-blocking I/O model. The first call to my_io_task() starts the timer and leaves it there. It does not wait for the response from the function. Instead, it moves on to call the second my_io_task(), starts the timer and leaves it there.

由于Node.js具有非阻塞I / O模型,因此花费的时间更少。 首次调用my_io_task()启动计时器并将其留在那里。 它不等待功能的响应。 相反,它继续调用第二个my_io_task() ,启动计时器并将其留在那里。

When the timer completes it’s execution taking 5 seconds, it calls the function and prints done on the console. Since both the timers are started together, they complete together and therefore take same amount of time.

计时器完成后,它需要5秒钟的时间执行,它会调用该函数并在控制台上打印done 。 由于两个计时器一起启动,因此它们一起完成,因此花费的时间相同。

套接字 (Socket.io)

Socket.io is a Node.js library made to help make real-time communication between computers possible. To ensure this Socket.io uses WebSockets to establish a connection between the client’s browser and the server. This library uses Engine.IO for building the connection.

Socket.io是一个Node.js库,旨在帮助实现计算机之间的实时通信。 为确保此效果,Socket.io使用WebSockets在客户端的浏览器和服务器之间建立连接。 该库使用Engine.IO建立连接。

演示版 (Demos)

To get a taste of what is possible, Socket.io provides two demos to show it’s possible use-cases. You can find the demos at https://socket.io/demos/chat/ and find the link to the whiteboard demo on the left.

为了了解可能发生的情况,Socket.io提供了两个演示来演示可能的用例。 您可以在https://socket.io/demos/chat/上找到演示,并在左侧找到指向白板演示的链接。

开始使用 (Get Started)

Since Socket.io is a Node.js library you have to make sure that Node.js is installed. If it’s not set up yet get the latest version at Nodejs.org

由于Socket.io是一个Node.js库,因此必须确保已安装Node.js。 如果尚未设置,请在Nodejs.org上获取最新版本

苹果系统 (macOS)

Node.js can also be installed via Homebrew a package manager for macOS.

还可以通过Homebrew macOS的软件包管理器安装Node.js。

Just type brew install node to install Node.js.

只需键入brew install node即可安装Node.js。

A get started guide can also be found on Socket.io’s page. It shows how to easily build a real-time chat in just a couple of lines.

也可以在Socket.io的页面上找到入门指南。 它显示了如何仅用几行就可以轻松地建立实时聊天。

更多信息 (More information)

More information about Socket.io and it’s documentation can be found at:

有关Socket.io及其文档的更多信息,请参见:

有关Node.js的更多信息 (More information on Node.js)

翻译自: https://www.freecodecamp.org/news/how-to-install-node-js-and-npm-on-windows/

 类似资料: