Jak zainstalować Node.js na Ubuntu 22.04
Zainstaluj Node.js na Ubuntu 22.04
Cel
Node.js jest jednym z najbardziej znanych asynchronicznych środowisk wykonawczych JavaScript sterowanych zdarzeniami. Jego popularność w ostatnich latach sprawiła, że stał się nieodzowną platformą w projektach deweloperskich. Aby dowiedzieć się więcej o możliwościach Node.js, sprawdź oficjalną dokumentację.
Z tego tutoriala dowiesz się, jak zainstalować i skonfigurować platformę Node.js na dystrybucji Linuksa Ubuntu 22.04 .
Wymagania
W tym tutorialu przyjmujemy, że posiadasz instancję Compute w ramach Public Cloud, serwer VPS lub serwer Bare Metal z systemem Ubuntu 22.04 oraz podstawowe umiejętności w zakresie wierszy poleceń. Na potrzeby tego tutorialu opieramy się na przykładzie instancji Compute w Public Cloud. Jeśli potrzebujesz pomocy w skonfigurowaniu instancji Public Cloud z systemem Ubuntu 22.04, skorzystaj z tego przewodnika: Tworzenie pierwszej instancji Public Cloud i łączenie się z nią.
Aby zainstalować managera Node.js, wcześniej należy zainstalować narzędzie make.
Wskazówki
Dzięki temu tutorialowi zainstalujesz platformę Node.js, nauczysz się jej używać i dowiesz się, jak przełączać się między kilkoma zainstalowanymi wersjami.
W momencie tworzenia tego tutoriala najnowsza wersja Node.js to wersja 16.15.x, a najnowsza wersja GA to 18.1.x.
Manager węzłów
Przed zainstalowaniem Node.js zainstaluj narzędzie do zarządzania wieloma instalacjami Node.js. Dwa najczęściej używane narzędzia to nvm i n. W tym przykładzie używamy n.
Aby zainstalować n w systemie Ubuntu, użyj następującego skryptu bash:
curl -L https://bit.ly/n-install | bash
Wynik wyświetli się w następujący sposób:
$ curl -L https://bit.ly/n-install | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 161 100 161 0 0 718 0 --:--:-- --:--:-- --:--:-- 718 100 43367 100 43367 0 0 83076 0 --:--:-- --:--:-- --:--:-- 225k === You are ABOUT TO INSTALL n, the Node.js VERSION MANAGER, in: /home/ubuntu/n Afterwards, THE FOLLOWING Node.js VERSION(S) WILL BE INSTALLED, and the first one listed will be made active; 'lts' refers to the LTS (long-term support) version, 'latest' to the latest available version. '-' means that *no* version will be installed: lts If your shell is bash, bsh, zsh, fish, or pwsh (PowerShell), the relevant initialization file will be modified in order to: - export environment variable $N_PREFIX. - ensure that $N_PREFIX/bin is in the $PATH Your shell, bash, IS supported, and the following initialization file will be updated: /home/ubuntu/.bashrc For more information, see https://bit.ly/n-install-repo === CONTINUE (y/N)? y -- Cloning https://github.com/tj/n to '/home/ubuntu/n/n/.repo'... -- Running local n installation to '/home/ubuntu/n/bin'... -- Shell initialization file '/home/ubuntu/.bashrc' updated. -- Installing helper scripts in '/home/ubuntu/n/bin'... -- Installing the requested Node.js version(s)... 1 of 1: lts... installing : node-v16.15.0 mkdir : /home/ubuntu/n/n/versions/node/16.15.0 fetch : https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz copying : node/16.15.0 installed : v16.15.0 (with npm 8.5.5) === n successfully installed. The active Node.js version is: v16.15.0 Run `n -h` for help. To update n later, run `n-update`. To uninstall, run `n-uninstall`. IMPORTANT: OPEN A NEW TERMINAL TAB/WINDOW or run `. /home/ubuntu/.bashrc` before using n and Node.js. ===
Sprawdź instalację:
n --version
Wynik wyświetli się w następujący sposób:
$ n --version v8.2.0
Instalacja Node.js z n
Aby zainstalować wersję LTS Node.js, wpisz następujące polecenie:
n lts
Wynik wyświetli się w następujący sposób:
$ n lts copying : node/16.15.0 installed : v16.15.0 (with npm 8.5.5)
Sprawdź nową instalację:
npm --version node --version
Wynik wyświetli się w następujący sposób:
$ npm --version 8.5.5 $ node --version v16.15.0
Aby zainstalować najnowszą wersję GA Node.js:
n current
Wynik wyświetli się w następujący sposób:
$ n current installing : node-v18.1.0 mkdir : /home/ubuntu/n/n/versions/node/18.1.0 fetch : https://nodejs.org/dist/v18.1.0/node-v18.1.0-linux-x64.tar.xz copying : node/18.1.0 installed : v18.1.0 (with npm 8.8.0)
Test instalacji Node.js
Przetestuj instalację Node.js, tworząc plik Hello World. Utwórz plik HelloWorld.js i wklej do niego następujący kod:
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}/`); });
Zapisz i uruchom go.
node HelloWorld.js
Wynik wyświetli się w następujący sposób:
$ node HelloWorld.js Server running at http://127.0.0.1:3000/
Aby przetestować próbkę, uruchom następujące polecenie:
curl http://127.0.0.1:3000/
Wynik wyświetli się w następujący sposób:
$ curl http://127.0.0.1:3000/ 👋 Hello World
To już wszystko, zainstalowałeś i skonfigurowałeś Node.js na Ubuntu 22.04.