首頁 > web前端 > js教程 > 主體

什麼是 Node.js

PHPz
發布: 2024-08-01 01:44:23
原創
807 人瀏覽過

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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!