Install NPM into home directory with distribution nodejs package (Ubuntu)

后端 未结 8 1668
忘了有多久
忘了有多久 2020-12-07 06:38

I\'d like to use the distribution Node.js packages (or the chris-lea ppa for more recent releases) but install NPM to my home directory.

This may seem picky, but it

8条回答
  •  感动是毒
    2020-12-07 07:43

    Jake's answer was posted in 2012 and while useful it references Chris Lea's Node.js PPAs who are no longer updated since march 2015.

    Here's the steps I use to install Node.js and npm in my home directory:

    Install Node.js with nvm (no sudo required):

    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
    

    Now you can install -g without sudo and everything goes into ~/.nvm/

    Or install Node.js without nvm (official instructions):

    Install Node.js

    • Node.js v6 (current LTS as of May 2017):

      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
      

    Change npm's default directory to a local one:

    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
    

    Alternatively replace .npm-global by the directory of your choice.

    Update npm and check it is installed in your $HOME directory:

    $ npm install npm -g
    /home//.npm-global/bin/npm -> /home//.npm-global/lib/node_modules/npm/bin/npm-cli.js
    /home//.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 
    

    Now you can install -g without sudo and without messing with your system files.

提交回复
热议问题