首页  >  文章  >  web前端  >  什么是 Node.js

什么是 Node.js

PHPz
PHPz原创
2024-08-01 01:44:23808浏览

What is Nodejs

Nodejs 是一个 JavaScript 运行时。这意味着nodejs是一个允许您在浏览器之外运行JavaScript的程序。这样一来,就可以使用nodejs来开发后端应用程序了。现在,这不仅限于后端。我们可以构建桌面应用程序、物联网和云应用程序等等。 Nodejs 是跨平台的。该程序本身可以运行 Linux、Windows 和 macOS。

为什么要使用 Nodejs

Nodejs 具有一些优势,包括但不限于:

  • 非阻塞 I/O
  • 异步
  • 可扩展
  • 事件驱动
  • 低延迟
  • 有线程
  • 随时随地都可以使用
  • 拥有一个庞大的社区

俗话说,眼前的回报意味着长期的不便。这里的缺点是 javascript(顺便说一句,我喜欢 javascript),有时在设计您想要构建的系统时没有考虑到扩展性。再说一遍,不是 Nodejs,而是使用 Nodejs 的工具和人类。

您可以在这里阅读有关 Nodejs 的更多信息

安装

Nodejs 的人很聪明,尊重这一点。它们使您和我的安装变得更加容易。没有技术知识的人可以设置 Nodejs 并开始编写一些代码。他们提供了可以使用的选项:

  • 包管理器
  • 预构建的安装程序
  • 预构建的二进制文件
  • 通过构建源代码进行安装

其中,前三个是友好的。所以选择其中任何一个。让我们前往 download-nodejs 并“放一个 Nodejs”。

截至目前,当前节点版本为 22,LTS(具有长期支持)为 20。

我使用的是 Linux 机器,所以我将使用 nvm(Node 版本管理器)进行安装。这让我们感觉我们可以拥有多个版本的 Nodejs。这对于 macOS 来说也是开箱即用的。

# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# download and install Node.js (you may need to restart the terminal)
nvm install 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`

这与nodejs平台(网站)上的脚本相同。所以运行这些命令时应该不会有任何问题。

对于 Windows,类似的东西将是

# installs fnm (Fast Node Manager)
winget install Schniz.fnm

# download and install Node.js
fnm use --install-if-missing 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`

或者只需下载预构建的安装程序,node-prebuilt-installer。最终,您应该能够运行最后两个命令来验证您的安装。

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`

非易失性存储器

在安装过程中,nvm 不是 Windows 的选项,但可以在此处安装,了解一点它会很有教育意义。

我们使用命令 nvm list 列出我们拥有的所有其他版本的 Nodejs

username@computer-name:~$ nvm list
->     v18.18.0
default -> 18.18.0 (-> v18.18.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.18.0) (default)
stable -> 18.18 (-> v18.18.0) (default)
lts/* -> lts/hydrogen (-> v18.18.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.18.0

从上面我们可以看出v18.18.0是我运行的nodejs。

我们可以使用 nvm install 20 安装其他版本,例如 20 LTS

username@computer-name:~$ nvm install 20
Downloading and installing node v20.15.1...
Downloading https://nodejs.org/dist/v20.15.1/node-v20.15.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.15.1 (npm v10.7.0)

这会自动切换到 v20.15.1。这是最新的 LTS。

现在我可以通过 nvm use 18 切换到我们想要的节点版本

username@computer-name:~$ nvm use 18
Now using node v18.18.0 (npm v10.8.2)
username@computer-name:~$
username@computer-name:~$ node -v
v18.18.0

这就是 nvm 上的

国家公共管理

npm 是一个节点包管理器。如果您想知道什么是包裹,请不要紧张。包与库相同。由其他人编写的一些代码片段或程序可以在我们的程序中用来执行某些操作。因此,包的目的是解决问题等等。 npm 和其他节点包管理器(如yarn、pnpm、bun 等)帮助我们管理为项目安装的包。我们在这里将只关注 npm。

要启动一个nodejs项目(不仅仅是javascript),我们需要使用node包。我的意思是,有时我们开发整个程序时不依赖第三方库(我们没有编写也不是 Nodejs 附带的程序)。

我们可以通过使用命令 npm init 创建节点 packege.json 文件来创建 Nodejs 应用程序。执行 npm init --help 来阅读有关 npm init 的更多信息。通常最好在新的环境(文件夹)中启动节点程序。所以我们将创建一个并将其命名为 helloworld。我将使用终端。

username@computer-name:~$ mkdir helloworld
username@computer-name:~$ cd helloworld/
username@computer-name:~/helloworld$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (helloworld) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/username/helloworld/package.json:

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": ""
}

Is this OK? (yes) 

username@computer-name:~/helloworld$ 
  • 我创建了一个名为 mkdir helloworld 的文件夹
  • 我改进了helloworld文件夹,cd helloworld
  • 然后我初始化节点,npm init

它就像一个安装向导,引导您完成配置步骤。请注意,您可以稍后更新。您只需按“ENTER”、“ENTER”,直到整个过程结束。当您在文件资源管理器中打开 helloworld 文件夹时,您将看到一个新文件 package.json ,其内容与上面的输出类似。

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": ""
}

这个配置很直观。它告诉您我们将要创建的项目(或程序)的名称。它使用父文件夹名称作为项目名称。在节点(项目)初始化过程中,我们可以给它一个名称,甚至为其他字段提供值。这就是我们按下 ENTER、ENTER、...

的地方

Another way to run through this without hitting ENTER, ENTER, …, is to do, npm init -y . -y, mean, yes, use the default values.

Primarily, node packages are on npmjs.com. Let’s say we want to install the expressjs library. This is how to search for express on npmjs. The docs will tell you how to install it using the command, npm i express.

username@computer-name:~/helloworld$ npm i express

added 64 packages, and audited 65 packages in 4s

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

i means install. You write it out as npm install express. The package.json will be updated with the package added.

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.19.2"
 }
}

Now, we have a new dependency.

Note that no file or folder will be created. When we do ls

username@computer-name:~/helloworld$ ls
node_modules  package.json  package-lock.json
  • We have node_modules, which is a folder that holds the dependencies (packages) our program will use.
  • Also we have, package-lock.json, which serves as a lockfile, hence the name. It captures the exact versions of the package that we install and use to create our programs. This way, the same packages and their specific versions can be used all the time since different versions of the same package may behave differently.

Anyways, we can install packages in three ways or rather environment. This is basically where you want the package to be used.

  • global: This will be accessible to all node programs you have. Usually, install packages globally when they are general purpose programs like command line utilities.
  • development: This is meant for development only and not used on some remote servers since the remote server will have its way of handling the use case of that dependency. These are usually utility libraries that work with other libraries to achieve a purpose. These may include but are not limited to eslint, prettier, dotenv, etc.
  • production: This is a package that our application primarily relies on to function. Like express.

We can do,

  • npm i -g package-names ... to globally install a package
  • npm i --global package-names ... to globally install a package
  • npm i -S package-names ... to install a package (for production)
  • npm i --save package-names ... to install a package (for production)
  • npm i -D package-names ... to install a package (for development, you won’t need it to make our application run)
  • npm i --save-dev package-names ... to install a package (for development, you won’t need it to make our application run)
  • npm uninstall package-names ... to remove or uninstall package

Essentially this is all that we will need to manage our packages.

以上是什么是 Node.js的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn