Centos7.1+Mac环境安装Node.Js以及彻底删除卸载node.js教程

Posted by Yancy on 2016-08-16

linux Centos7系统快速安装node.js npm

1
2
3
4
5
6
7
第一、选择合适的版本安装
# 4.x版本
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
# 5.x版本
curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
# 6.x版本
curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -

如果是Centos7的话会提示报错Requires: libhttp_parser.so.2()(64bit)

1
2
3
4
5
6
7
8
9
10
11
12
13
rpm -ivh https://rpm.nodesource.com/pub_6.x/el/7/x86_64/nodejs-6.2.0-1nodesource.el7.centos.x86_64.rpm
[root@H5-nha ~]# node -v
v6.2.0
[root@H5-nha ~]# npm -v
3.8.9
rpm -ivh https://kojipkgs.fedoraproject.org//packages/http-parser/2.7.1/3.el7/x86_64/http-parser-2.7.1-3.el7.x86_64.rpm && yum -y install nodejs
[root@logstash kibana-5.5.2]# node -v
v6.11.1
[root@logstash kibana-5.5.2]# npm -v
3.10.10
1
2
Centos6安装命令:
rpm -ivh https://kojipkgs.fedoraproject.org/packages/http-parser/2.6.0/2.fc24/x86_64/http-parser-2.6.0-2.fc24.x86_64.rpm && yum -y install nodejs

Mac OS安装:

安装报错304:

安装Node.Js 报错 340 解决方法
Fixing npm On Mac OS X for Homebrew Users
If you just want to fix the issue quickly, scroll down to the “solution” section below.
Explanation of the issue
If you’re a Homebrew user and you installed node via Homebrew,
there is a major philosophical issue with the way Homebrew and NPM work together.
If you install node with Homebrew and then try to do npm update npm -g, you may see an error like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ npm update npm -g npm http GET https://registry.npmjs.org/npm
npm http 304 https://registry.npmjs.org/npm
npm http GET https://registry.npmjs.org/npm/1.4.4
npm http 304 https://registry.npmjs.org/npm/1.4.4
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/nodemodules/npm
npm ERR! error rolling back at clobberFail (/usr/local/Cellar/node/0.10.26/lib/nodemodules/npm/lib/utils/gently-rm.js:57:12)
npm ERR! error rolling back at next (/usr/local/Cellar/node/0.10.26/lib/nodemodules/npm/lib/utils/gently-rm.js:43:14)
npm ERR! error rolling back at /usr/local/Cellar/node/0.10.26/lib/nodemodules/npm/lib/utils/gently-rm.js:52:12
npm ERR! error rolling back at Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back npm@1.4.4 { [Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/nodemodules/npm]
code: 'EEXIST', path: '/usr/local/bin/npm' }
npm ERR! Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/nodemodules/npm
File exists: /usr/local/bin/npm
Move it away, and try again.
npm ERR! System Darwin 13.1.0
npm ERR! command "/usr/local/Cellar/node/0.10.26/bin/node" "/usr/local/bin/npm" "update" "npm" "-g"
npm ERR! cwd /Users/dan/Google Drive/Projects/dotfiles
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /usr/local/bin/npm
npm ERR! code EEXIST
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/dan/Google Drive/Projects/dotfiles/npm-debug.log
npm ERR! not ok code 0

There’s an NPM bug for this exact problem. The bug has been “fixed”
by Homebrew installing npm in a way that allows it to manage itself once the install is complete. However,
this is error-prone and still seems to cause problems for some people.
The root of the the issue is really that npm is its own package manager and
it is therefore better to have npm manage itself and its packages completely on its own instead of letting Homebrew do it.

Also, using the Homebrew installation of npm will require you to use sudo when installing global packages.
Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access,
this is a bad idea.

Solution

This solution fixes the error caused by trying to run npm update npm -g.
Once you’re finished, you also won’t need to usesudo to install npm modules globally.
Before you start, make a note of any globally installed npm packages.
These instructions will have you remove all of those packages.
After you’re finished you’ll need to re-install them.
Run the following commands to remove all existing global npm modules, uninstall node & npm,
re-install node with the right defaults, install npm as its own pacakge,
and configure the location for global npm modules to be installed.

1
2
3
4
5
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.node >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

这里的安装脚本可以看我的GitHub

Node and npm should be correctly installed at this point.
The final step is to add ~/.node/bin to your PATH so commands you install globally are usable.
I added this line to my ~/.path script, which gets run via ~/.bash_profile.

设置变量:

1
export PATH="$HOME/.node/bin:$PATH"

Now you can re-install any global packages with the command below,
replacing the npm modules with whatever global packages you need.

测试npm安装http-server:

1
npm install -g http-server node-inspector forever nodemon