虚拟机下的CentOS环境中安装Node.js
本來這個并沒有什么好寫的,為什么,網上到處都是。不過在安裝的時候確實遇到了很多問題,很有必要記錄一下。
?
先安裝gcc-c++編譯環境和openssl,
>yum install gcc-c++ openssl-devel
第二點,不要使用最新版本的node.js(官網目前最新的為0.5.5),使用最新的在安裝express模塊會提示錯誤,為了避免這個問題還是裝個0.5以上0.4以上的版本最好,我使用的是ver 0.4.8
>wget?http://nodejs.org/dist/node-v0.4.8.tar.gz
?
>tar –xvf node-v0.4.8.tar.gz
>cd node-v0.4.8
>./configure;make;make install
如果在使用./configure時報錯:Node.js could not configure a cxx comiler!,解決辦法:
>yum groupinstall "Development Tools”
?
寫個例子,測試一下node.js是否能正常運行
var http = require('http');http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
將以上代碼存為hello.js,然后使用node hello.js運行,在控制臺上按住ctrl鍵,會出現下劃線直接點擊。如果打開的網頁不能直接瀏覽,則將上面代碼中的127.0.0.1 –> 換成機器的IP地址
?
?
然后準備安裝npm
>curl?http://npmjs.org/install.sh?| sh
?
然后安裝express模塊
npm install express
再安裝socket.io,這時出問題了…
類似這樣的錯誤:
ERR! Error: Failed tar "-mvxpf" "-" "-o" "-C" "/tmp/npm-1313089924364/1313089933124-0.37383073731325567/contents/___package.npm"
ERR! exited with 2
ERR! at ChildProcess. (/usr/local/lib/node_modules/npm/lib/utils/exec.js:85:8)
ERR! at ChildProcess.emit (events.js:67:17)
ERR! at Socket. (child_process.js:172:12)
ERR! at Socket.emit (events.js:64:17)
ERR! at Array.1 (net.js:831:12)
ERR! at EventEmitter._tickCallback (node.js:126:26)
ERR! Report this?entire?log at:
ERR!?http://github.com/isaacs/npm/issues
ERR! or email it to:
ERR!?npm-@googlegroups.com
ERR!
ERR! System Linux 2.6.18-194.26.1.el5.028stab081.1ent
ERR! command "node" "/usr/local/bin/npm" "install" "socket.io"
ERR! cwd /root
ERR! node -v v0.4.10
ERR! npm -v 1.0.22
?
?
找了半天沒發現什么,google搜索出來的結果說是tar的版本過低了,使用tar --version查看一下,為1.15.3,怎么辦?下載最新的版本
先是使用yum install tar,得到的結果是沒有需要更新的
然后開始下載最新版本的tar(http://www.gnu.org/software/tar/)
wget?http://ftp.gnu.org/gnu/tar/tar-1.26.tar.gz
然后解壓,安裝,可是很郁悶的是發現也報錯了。
checking whether mknod can create fifo without root privileges... > configure: error: in `/usr/xxx/tar-1.26': > configure: error: you should not run configure as root (set > FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check) > See `config.log' for more details.?
還好找到這篇文章:安裝GNU tar?(需要使用強制執行,忽略非安全的check)
是自己沒有仔細看報錯的界面,按照上面的說法,使用
>/configure FORCE_UNSAFE_CONFIGURE=1
重新check、make、install然后搞定
這個時候的tar版本就為最新的1.26了,然后再安裝socket.io就正常了
?
然后參照socket.io的官網,寫個例子測試一下,看環境是否OK了?
轉載于:https://www.cnblogs.com/fzsh/archive/2011/11/15/2249767.html
總結
以上是生活随笔為你收集整理的虚拟机下的CentOS环境中安装Node.js的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于C#资源文件操作的总结
- 下一篇: 不可能解开的谜题 (程序员修炼之道,