code

배포 nodejs 패키지 (Ubuntu)를 사용하여 홈 디렉터리에 NPM 설치

codestyles 2020. 9. 23. 07:38
반응형

배포 nodejs 패키지 (Ubuntu)를 사용하여 홈 디렉터리에 NPM 설치


배포 Node.js 패키지 (또는 최신 릴리스의 경우 chris-lea ppa )를 사용하고 싶지만 내 홈 디렉토리에 NPM을 설치합니다.

이것은 까다로워 보일 수 있지만 다중 언어 / github를 사용하는 개발자가 Linux에서 언어 런타임 / 라이브러리 환경을 설정하는 매우 관용적 인 방법입니다. 런타임 용 배포 패키지, 사용자 별 환경의 타사 라이브러리 (virtualenv, RVM-RVM 참조) 원한다면 Ruby도 빌드 할 것입니다). 필요한 경우 노드를 로컬로 구축 할 것이지만 노드가 많은 프로젝트에 대한 부수적 인 개발 요구 사항이되고 있으므로 PITA입니다.


NPM은 이미 프로젝트에 로컬 패키지를 설치하지만 여전히 시스템을 내 운영 체제의 파일에서 멀리하고 싶습니다. Nodejs 패키지를 구획화하는 방법은 다음과 같습니다.

chris-lea PPA를 통해 Nodejs 및 NPM을 설치합니다. 그런 다음 내 homedir에 패키지 루트를 설정하여 노드 "글로벌"패키지를 보유합니다.

 $ NPM_PACKAGES="$HOME/.npm-packages"
 $ mkdir -p "$NPM_PACKAGES"

글로벌 패키지 설치에이 디렉토리를 사용하도록 NPM을 설정하십시오.

 $ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc

.zshrc / .bashrc에 다음을 추가하여 $ NPM_PACKAGES 접두사에서 명령을 볼 수 있도록 PATH 및 MANPATH를 구성합니다.

# NPM packages in homedir
NPM_PACKAGES="$HOME/.npm-packages"

# Tell our environment about user-installed node tools
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH  # delete if you already modified MANPATH elsewhere in your configuration
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"

# Tell Node about these packages
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"

이제이 작업을 수행 할 때 npm install -g, NPM은에 라이브러리를 설치합니다 ~/.npm-packages/lib/node_modules, 그리고으로 실행 도구를 연결 ~/.npm-packages/bin하여에 인 PATH.

npm install -g평소처럼 사용하십시오 .

[justjake@marathon:~] $ npm install -g coffee-script
... (npm downloads stuff) ...
/home/justjake/.npm-packages/bin/coffee -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/coffee
/home/justjake/.npm-packages/bin/cake -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/cake
coffee-script@1.3.3 /home/justjake/.npm-packages/lib/node_modules/coffee-script

[justjake@marathon:~] $ which coffee
/home/justjake/.npm-packages/bin/coffee

Jake의 답변은 2012 년에 게시되었으며 유용하지만 2015 년 3 월 이후 더 이상 업데이트되지 않는 Chris Lea의 Node.js PPA를 참조합니다.

내 홈 디렉토리에 Node.js 및 npm을 설치하는 데 사용하는 단계는 다음과 같습니다.

nvm으로 Node.js를 설치합니다 ( sudo필수 없음 ).

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
nvm install 7
npm install -g npm  # update npm

이제 당신은 install -g없이 할 수 sudo있고 모든 것이 들어갑니다.~/.nvm/

또는 nvm없이 Node.js를 설치합니다 ( 공식 지침 ) :

Node.js 설치

  • Node.js v6 (2017 년 5 월 현재 LTS) :

    curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    sudo apt-get install -y nodejs
    
  • Node.js v7 :

    curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

npm의 기본 디렉토리를 로컬 디렉토리로 변경합니다.

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH"  # ← put this line in .bashrc
source ~/.bashrc  # if you only updated .bashrc

또는 .npm-global선택한 디렉토리로 대체하십시오 .

npm을 업데이트하고 $HOME디렉토리에 설치되어 있는지 확인하십시오 .

$ npm install npm -g
/home/<username>/.npm-global/bin/npm -> /home/<username>/.npm-global/lib/node_modules/npm/bin/npm-cli.js
/home/<username>/.npm-global/lib
└─┬ npm@3.10.6 
  ├─┬ glob@7.0.5 
  │ └── minimatch@3.0.2 
  ├── npm-user-validate@0.1.5 
  └── rimraf@2.5.3 

이제 시스템 파일을 엉망으로 install -g만들지 않고도 할 수 sudo있습니다.


The solution posted by Just Jake is great. However, due to a bug with npm > 1.4.10, it may not work as expected. (See this and this)

While the bug is solved, you can downgrade to npm 1.4.10 by following this steps:

  1. Comment the prefix line in your $HOME/.npmrc
  2. Run sudo npm install -g npm@1.4.10
  3. Ensure that the right version of npm is installed (npm --version)
  4. Uncomment the prefix line in your $HOME/.npmrc
  5. Proceed to install your global packages in your home folder!.

Because python does already a great job virtualenv, I use nodeenv. Compared to nvm, you can create multiple environments for the same node version (e.g. two environments for node 0.10 but with different sets of packages).

ENVNAME=dev1

#  create an environment
python -m virtualenv ${ENVNAME}

# switch to the newly created env
source ${ENVNAME}/bin/activate

# install nodeenv
pip install nodeenv

# install system's node into virtualenv
nodeenv --node=system --python-virtualenv

The readme is pretty good: https://github.com/ekalinin/nodeenv


I used @just-jake solution for some time and found that nvm is easier to setup. Also it's much powerful solution that allows to install and use different versions of nodejs.

On Ubuntu 14.04 or 16.04:

  1. Install prerequisite packages for building nodejs:

    sudo apt-get update
    sudo apt-get install build-essential libssl-dev
    
  2. Install nvm:

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
    

    In case newer version of nvm will be available you can find actual installation command on nvm site.

  3. nvm installer will add bootstrap script to ~/.bashrc, so you need either to reopen terminal to run it, or to do:

    source ~/.bashrc
    
  4. Now you can install any nodejs version you like, switch between them etc.

    Use nvm ls-remote to list available nodejs versions.

    To install, for example, nodejs v4.2.4 do:

    # install v4.2.4
    nvm install v4.2.4 
    # use nodejs v4.2.4 in the current terminal session
    nvm use v4.2.4
    # use v4.2.4 by default in new terminal session
    nvm alias default v4.2.4
    

To expand on the answer provided by Just Jake and user1533401: I am unable to downgrade as I use shared hosting and node is installed in a system directory. This is also why I have change the directory where npm installs global scripts if I want it to do that. For those in the same boat, here is a another temporary fix I found works:

npm install -g --prefix=$(npm config get prefix) <package>

The bug is that npm doesn't read your per-user config file, but specifying it every time you install a global script fixes that. Found here.


As stated already here and here

npm config set prefix ~
echo export PATH=\$PATH:\~/bin >> ~/.bashrc
. ~/.bashrc

참고URL : https://stackoverflow.com/questions/10081293/install-npm-into-home-directory-with-distribution-nodejs-package-ubuntu

반응형