不幸的是,我不能只输入“npm install”并安装包。我必须使用'sudo npm install'。你知道如何在不以管理员身份运行的情况下让 npm 工作吗?
Uninstall any existing instances:
sudo npm uninstall npm -g
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
sudo rm -rf /usr/local/bin/npm /usr/local/share/man/man1/node* /usr/local/lib/dtrace/node.d ~/.npm ~/.node-gyp /opt/local/bin/node opt/local/include/node /opt/local/lib/node_modules
Run Code Online (Sandbox Code Playgroud)
Install any missing required libraries:
sudo apt-get install build-essential g++ flex bison gperf ruby perl libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev libpng-dev libjpeg-dev python libx11-dev libxext-dev
sudo apt-get install python-dev
Run Code Online (Sandbox Code Playgroud)
Download and install nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
Run Code Online (Sandbox Code Playgroud)
Ensure permanent access from every bash instance:
sudo gedit ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)
when the file opens in the editor, type the following, save and exit gedit:
export NVM_DIR="/home/your_username/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Run Code Online (Sandbox Code Playgroud)
next, run:
nvm ls-remote
Run Code Online (Sandbox Code Playgroud)
and choose a version you like from the list (I prefer current LTS). Then install it like this:
nvm install 6.11.0
Run Code Online (Sandbox Code Playgroud)
check if everything is ok:
npm -v
Run Code Online (Sandbox Code Playgroud)
now you should be able to run npm without sudo