본문 바로가기

리눅스

[리눅스] nvm, node(npm) 설치

728x90

nvm, node(npm) 설치

- NVM(Node Version Manager)

- NPM(Node Package Manager)

nvm 설치

nvm 스크립트를 다운로드하여 설치합니다.

curl -s -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
$ curl -s -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
=> nvm is already installed in /root/.nvm, trying to update the script

=> nvm source string already in /root/.bashrc
=> bash_completion source string already in /root/.bashrc
=> 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

.bashrc 파일 확인

$ cat /root/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

PS1=$PS1A

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 디렉터리 확인

$ ls -al | egrep .nvm
drwxr-xr-x 2 root root 4096 Jul 21 23:51 .nvm
source .bashrc

nvm version

nvm --version
$ nvm --version
0.39.1

nvm으로 node(npm) 설치하기

release(node) 된 전체 버전 목록 출력(node 버전 확인)

nvm ls-remote

lts(Long Term Support) 버전만 목록 출력

nvm ls-remote --lts
$ nvm ls-remote | egrep Latest
         v4.9.1   (Latest LTS: Argon)
        v6.17.1   (Latest LTS: Boron)
        v8.17.0   (Latest LTS: Carbon)
       v10.24.1   (Latest LTS: Dubnium)
      v12.22.12   (Latest LTS: Erbium)
       v14.20.0   (Latest LTS: Fermium)
       v16.16.0   (Latest LTS: Gallium)

node 설치

nvm install 16.16.0
$ nvm install 16.16.0
Downloading and installing node v16.16.0...
Downloading https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.16.0 (npm v8.11.0)
Creating default alias: default -> 16.16.0 (-> v16.16.0)

로컬에 설치되어 있는 node 목록 출력

$ nvm ls
       v16.16.0
default -> 16.16.0 (-> v16.16.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.16.0) (default)
stable -> 16.16 (-> v16.16.0) (default)
lts/* -> lts/gallium (-> v16.16.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.20.0 (-> N/A)
lts/gallium -> v16.16.0

node version

node --version
$ node --version
v16.16.0

npm version

npm -v
$ npm -v
8.11.0

node 12 버전 추가 설치

 - minor와 patch 버전은 생략 가능함

 - 마지막으로 설치한 버전으로 사용할 버전이 지정됨

nvm install 12
$ nvm install 12
Downloading and installing node v12.22.12...
Downloading https://nodejs.org/dist/v12.22.12/node-v12.22.12-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.12 (npm v6.14.16)
nvm ls
$ nvm ls
      v12.22.12
       v16.16.0
default -> 16.16.0 (-> v16.16.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.16.0) (default)
stable -> 16.16 (-> v16.16.0) (default)
lts/* -> lts/gallium (-> v16.16.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
lts/fermium -> v14.20.0 (-> N/A)
lts/gallium -> v16.16.0

node 버전을 변경하여 사용(임의로 node 16으로 변경하고 다시 변경하였음)

$ nvm use 12
Now using node v12.22.12 (npm v6.14.16)
$ node -v
v12.22.12
$ npm -v
6.14.16

npm 목록 확인

npm ls
$ npm ls
/root
`-- (empty)

node.js 기본 웹 올리기

프로젝트 디렉터리 생성

mkdir node-www
cd node-www/

npm 초기화

npm init
$ 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: (www)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /root/node-www/package.json:

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


Is this OK? (yes) yes
npm notice
npm notice New minor version of npm available! 8.11.0 -> 8.15.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.15.0
npm notice Run npm install -g npm@8.15.0 to update!
npm notice
npm ls
$ npm ls
www@1.0.0 /root/node-www
`-- (empty)
$ ls -l | grep package.json
-rw-r--r-- 1 root root 199 Jul 22 00:47 package.json

 

index.js 파일 편집(생성)

$ vi index.js
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}/`);
});

index.js code >> https://nodejs.org/en/docs/guides/getting-started-guide/

node.js  서버 시작

node index.js
$ node index.js
Server running at http://127.0.0.1:3000/

curl 접속 테스트

docker exec centos7 sh -c "curl -s localhost:3000"
$ docker exec centos7 sh -c "curl -s localhost:3000"
Hello World
728x90