본문 바로가기

: Front-end

nodenv 이용해서 npm 설치하기

반응형

※ nodenv란? (What is nodenv)

우선 nodenv는 node version control system 이다. 한 피씨에서, 여러 노드 버전을 이용하기 위해서 사용하는 것이다.
한 프로젝트만 진행하는 경우에는 별 상관이 없지만, 여러 프로젝트를 동시에 진행하다보면, npm version dependencies가 생기기 마련이고 이를 해결하기 위함이다.

※ node install --global 에러 해결 방법

Permission error
Missing write access to /usr/local/lib/node_modules

1차원적으로는 sudo 권한주면 해결 될 것 같은데, 따로 폴더 만들어서 관리하는 것을 추천
docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

 

Resolving EACCES permissions errors when installing packages globally | npm Docs

Documentation for the npm registry, website, and command-line interface

docs.npmjs.com

참고로 옵션 A, B 둘 다 적용 안되니, 보고 하나씩 진행하세요.
둘 다 하게 되면, B가 다 덮어쓸 듯?

옵션 A. 위 npmdocs의 첫번째 방법 이용 (Recommended)

github.com/nodenv/nodenv

  • 설치 방법

(this guide is for mac users)

brew install nodenv vi .zshrc # eval "$(nodenv init -)" curl -fsSL https://github.com/nodenv/nodenv-installer/raw/master/bin/nodenv-doctor | bash 

 

마지막 curl로 확인을 마친 다음에,

# list all available versions: $ nodenv install -l # install a Node version: $ nodenv install 14.16.1 # 버전 변경 $ nodenv global 14.16.1 # 해당 버전으로 변경됐는지 확인 $ nodenv version # shims path 등록 (Mac = zshrc) $ echo "PATH=${HOME}/.nodenv/shims:${PATH}" >> ~/.zshrc $ source ~/.zshrc # .../.nodenv/versions/14.16.1/lib/node_modules ".nodenv"폴더 맞는지 경로 확인 $ npm root -g $ npm install -g semver # 확인 $ semver

 

옵션 B. Manually change npm's default directory

mkdir ~/.npm/global npm config set prefix '~/.npm/global' # echo export PATH=~/.npm/global/bin:$PATH >> ~/.zshrc # 위에처럼 해도되는데, 쉘 설정파일(.zshrc)에 들어가서 고치는게 나음 # export PATH=~/.npm/global/bin:$PATH source ~/.zshrc npm install --global semver

 


If you use intelliJ, check the below

https://github.com/nodenv/jetbrains-npm

 


 

 

※ nodenv 설명

github(github.com/nodenv/nodenv) 있는거 그대로 가져와, 한국어로 재풀이하였을 뿐입니다.
가능하시면, github을 읽으시는걸 추천드립니다.


How it works

nodenv는 $PATH 를 이용한 shim이라는 것을 통해 node commands를 가로챕니다.
그리고 어떤 node version이 지정할지 결정하고, 명령을 설치된 노드에 전달합니다.


Understanding PATH

"node" 또는 "npm" 이라는 명령어를 실행하기 위해, OS는 적합한 실행파일이 있는지 검색해야한다. 이것을 검색하는 디렉토리 리스트를 설정해두는 것이 PATH이다. 디렉토리는 여러 곳에 분산되어 있으며, colon(:)을 이용해서 분리되어져 있다.
예를들어,

PATH=/usr/local/bin:/usr/bin:/bin

검색을 할때는 왼쪽에서 오른쪽으로 검색을 한다. 즉 적합한 명령어가 여러개라면, 앞에 등록된 폴더 안의 명령을 우선한다.
이 경우에는 usr/local/bin에서 적합한 명령어를 우선 탐색한 후, usr/bin, 그 다음 /bin을 탐색한다.


Understanding Shims

shim의 원래 뜻은 두 물체 사이의 메꾸는 나무조각같은 것을 의미한다.
nodenv는 PATH 앞에 shims 디렉토리를 삽입하여 작동합니다.

~/.nodenv/shims:/usr/local/bin:/usr/bin:/bin

앞서 Understanding PATH에서 얘기한 것처럼, 앞에가면 기존 npm보다 우선순위가 높아지게 된다.
shims 은 nodenv에 전달하는 실행 파일들이 모인 폴더입니다.
npm을 입력했을 때 OS에서 하는 일을 다시 요약하면,

  1. PATH에서 npm이라는 실행 파일을 검색하십시오.
  2. PATH 시작 부분에서 npm이라는 nodenv shim을 찾습니다.
  3. npm이라는 shim을 실행하면 명령이 nodenv에 전달됩니다.

 


Locating the Node Installation

Each Node version is installed into its own directory under ~/.nodenv/versions.

 

 


FYI)

추가로 프록시, 원격 저장소(remote repository)를 변경하는 것은

$ npm config set proxy http://${ip}:${port} $ npm config set https-proxy http://${ip}:${port} $ npm config set strict-ssl false $ npm config set registry http://registry.npmjs.org


처럼 입력하면 되고,
확인하려면 .npmrc 파일을 확인하면 됨
($ npm config list) 입력해도 확인할 수 있음

반응형