找回密碼
 註冊
搜索
查看: 713|回復: 0

[AlamLinux] 如何在 AlmaLinux 9 上安裝 Node.js

[複製鏈接]
發表於 2024-10-4 16:13:19 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
步驟#1。保持AlmaLinux伺服器最新
在安裝 Node.js 之前,最好確保您的系統是最新的。執行以下命令來更新您的AlmaLinux系統:
  1. dnf update
複製代碼

此命令將更新系統的軟體包清單並將已安裝的軟體包更新至最新版本。


步驟#2。安裝必要的套件
若要安裝 Node.js 安裝所需的必要套件,請執行以下命令:
  1. dnf install curl dnf-plugins-core
複製代碼


步驟#3。列出可用的 Node.js 流
要列出可用的 Node.js 流,您可以執行以下命令:
  1. dnf module list nodejs
複製代碼

此命令將為您提供可供選擇的 Node.js 流清單。
這是輸出:

AlmaLinux Nodejs

AlmaLinux Nodejs


請注意,根據上面顯示的輸出,AlmaLinux 9中可透過DNF安裝的Node.js版本是Node.js 18節點版本管理器(NVM) 安裝Node.js 的步驟4b 部分。


步驟#4。使用DNF或NVM在Linux上安裝Node.js (AlmaLinux)


步驟#4a。使用DNF在Linux上安裝Node.js (AlmaLinux)
首先,選擇您想要在AlmaLinux系統上安裝的Node.js版本,然後執行以下命令完成安裝:
  1. sudo dnf module install nodejs:18
複製代碼

當提示匯入儲存庫的 GPG 金鑰時輸入y ,然後按Enter鍵。
這是輸出:

AlmaLinux Nodejs

AlmaLinux Nodejs

執行以下任意指令檢查 Node.js 是否安裝正確:

  1. node -v
複製代碼

這是輸出:
v18.18.2

NPM(節點套件管理器)包含在 Node.js 中,不得單獨安裝。使用以下任意指令顯示目前的 NPM 版本:
  1. npm -v
複製代碼


最後
  1. dnf module list nodejs
複製代碼

AlmaLinux Nodejs

AlmaLinux Nodejs

解除node.js
  1. dnf module remove nodejs:18
複製代碼

重置node.js 版本
  1. dnf module reset nodejs:18
複製代碼

安裝node.js 20版本
  1. dnf module install nodejs:20
複製代碼

建立檔案
  1. nano helloworld.js
複製代碼


接下來你可以從這裡複製 Nodejs

  1. const http = require('http');

  2. const hostname = '0.0.0.0';
  3. const port = 3000;

  4. const server = http.createServer((req, res) => {
  5.   res.statusCode = 200;
  6.   res.setHeader('Content-Type', 'text/plain');
  7.   res.end('Hello World');
  8. });

  9. server.listen(port, hostname, () => {
  10.   console.log(`Server running at http://${hostname}:${port}/`);
  11. });
複製代碼


運行 Nodejs 程式碼
  1. node helloworld.js
複製代碼




瀏覽器看輸出
  1. http://localhost:3000
複製代碼




參考文章
https://www.liquidweb.com/blog/install-node-js-linux-almalinux/
https://www.linuxcloudvps.com/bl ... de-js-on-almalinux/
 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-6-16 02:59 , Processed in 0.024864 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表