개발/Node.js

node.js linux 설치(install)

강서버 2024. 6. 8. 02:20
반응형

Node.js 설치 버전(install version) : v22.2.0(Current)

--------------------------------------------------------------------------------------------------------------------

# 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 22

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

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

--------------------------------------------------------------------------------------------------------------------

 

[ 설치 로그(install log) ]

.설치 환경(install environment) : 하모니카(HamoniKR) 7.0 - (http://hamonikr.org)

 

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16555  100 16555    0     0  26358      0 --:--:-- --:--:-- --:--:-- 26361
=> Downloading nvm from git to '/home/gangserver/.nvm'
=> '/home/gangserver/.nvm'에 복제합니다...
remote: Enumerating objects: 365, done.
remote: Counting objects: 100% (365/365), done.
remote: Compressing objects: 100% (313/313), done.
remote: Total 365 (delta 43), reused 163 (delta 26), pack-reused 0
오브젝트를 받는 중: 100% (365/365), 365.09 KiB | 7.16 MiB/s, 완료.
델타를 알아내는 중: 100% (43/43), 완료.
* (HEAD FETCH_HEAD 위치에서 분리됨)
  master
=> Compressing and cleaning up git repository

=> nvm source string already in /home/gangserver/.zshrc
=> bash_completion source string already in /home/gangserver/.zshrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

 

nvm 프로그램 설치가 완료되면 사용자 홈 디렉토리 아래 .nvm 폴더가 생성되고 nvm 프로그램 관련 파일이 그 안에 있다.

[  사용자 홈/.nvm 폴더 ]

 

그리고 설치 완료 시

nvm 관련 환경 설정이 .zshrc 파일 안에 추가되는데

우분투와 같이 bash 쉘을 사용하는 경우

아래 부분을 .bashrc 파일 맨 아래에 추가한다.

 

$ vi .bashrc

--------------------------------------------------------------------------------------------------------------------

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

--------------------------------------------------------------------------------------------------------------------

 

저장 후 vi 에티터 나온 후

$ . .bashrc 또는 $ sh .bashrc

실행 후 터미널 종류 후 터미널 재실행하여 아래와 같이 node.js v22 버전을 설치한다.

 

$ nvm install 22
Downloading and installing node v22.2.0...
Downloading https://nodejs.org/dist/v22.2.0/node-v22.2.0-linux-x64.tar.xz...
######################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.2.0 (npm v10.7.0)
Creating default alias: default -> 22 (-> v22.2.0)

 

nodejs의 버전을 확인한다.

$ node -v
v22.2.0

npm의 버전을 확인한다.
$ npm -v
10.7.0

 

 

테스트

아래 내용으로 hello.js 로 저장한다.

--------------------------------------------------------------------------------------------------------------------

var http = require('http')

http.createServer(function(request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8000);

console.log('Server running at http://localhost:8000/');

--------------------------------------------------------------------------------------------------------------------

 

hello.js 파일이 저장된 디렉토리에서 터미널을 실행하여 nodejs로 hello.js를 실행한다.

 

$ node hello.js

 

터미널에 "Server running at http://localhost:8000/" 메시지가 정상적으로 보이면

 

브라우저(ex, 크롬, 엣지, 파이어폭스 등)을 실행하여

주소란에 http://localhost:8000을 입력하면

 

와 같이 화면에 "Hello World"가 정상적으로 보이면 정상 실행된 경우이다.

 

 

참조 : https://nodejs.org/en/download/package-manager

 

Node.js — Download Node.js®

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

 

반응형