Node.js Development Environment—Ubuntu

Content:

     1. Installed via apt-get nodejs – Failed.

     2. Download the source code from github install – successful.

     3. Establish express project, start your first project.

 

1. Installation via apt-get nodejs – Failed
Set up the environment

 
 
  1. ~ sudo apt-get install nodejs
  2. ~ sudo apt-get install npm
  3. ~ node -v
  4. v0.6.12
  5. ~ npm -v 1.1.4

create working directory

 
 
  1. ~ mkdir workspace
  2. ~ mkdir workspace/nodejs
  3. ~ cd workspace/nodejs
  4. ~ pwd
  5. /home/vivianzhang/workspace/nodejs

Failed installation
Errors Conan got:

 
 
  1. sudo npm install express -g
  2. npm http GET https://registry.npmjs.org/express
  3. npm http 304 https://registry.npmjs.org/express
  4. npm http GET https://registry.npmjs.org/connect/2.7.11
  5. npm http GET https://registry.npmjs.org/commander/0.6.1
  6. npm http GET https://registry.npmjs.org/range-parser/0.0.4
  7. npm http GET https://registry.npmjs.org/mkdirp/0.3.4
  8. npm http GET https://registry.npmjs.org/cookie/0.1.0
  9. npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
  10. npm http GET https://registry.npmjs.org/fresh/0.1.0
  11. npm http GET https://registry.npmjs.org/methods/0.0.1
  12. npm http GET https://registry.npmjs.org/send/0.1.0
  13. npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
  14. npm http GET https://registry.npmjs.org/debug
  15. npm http 304 https://registry.npmjs.org/commander/0.6.1
  16. npm http 304 https://registry.npmjs.org/connect/2.7.11
  17. npm http 304 https://registry.npmjs.org/range-parser/0.0.4
  18. npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
  19. npm http 304 https://registry.npmjs.org/cookie/0.1.0
  20. npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
  21. npm http 304 https://registry.npmjs.org/fresh/0.1.0
  22. npm http 304 https://registry.npmjs.org/methods/0.0.1
  23. npm http 304 https://registry.npmjs.org/send/0.1.0
  24. npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
  25. npm http 304 https://registry.npmjs.org/debug
  26. npm ERR! error installing express@3.2.6
  27. npm ERR! error rolling back express@3.2.6 Error: UNKNOWN, unknown error '/usr/local/lib/node_modules/express'
  28.  
  29. npm ERR! Unsupported
  30. npm ERR! Not compatible with your version of node/npm: connect@2.7.11
  31. npm ERR! Required: {"node":">= 0.8.0"}
  32. npm ERR! Actual: {"npm":"1.1.4","node":"0.6.12"}
  33. npm ERR!
  34. npm ERR! System Linux 3.5.0-23-generic
  35. npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
  36. npm ERR! cwd /home/conan/workspace/nodejs
  37. npm ERR! node -v v0.6.12
  38. npm ERR! npm -v 1.1.4
  39. npm ERR! code ENOTSUP
  40. npm ERR! message Unsupported
  41. npm ERR! errno {}
  42. npm http GET https://registry.npmjs.org/mime/1.2.6
  43. npm ERR!
  44. npm ERR! Additional logging details can be found in:
  45. npm ERR! /home/conan/workspace/nodejs/npm-debug.log
  46. npm not ok

Prompt indicates that node and npm version are incompatible. It may be caused by failing to update the source by “apt-get”. And you should move on and install compatible node and npm manually.

Errors Vivian got:

 
 
  1. vivianzhang@ubuntu:~/workspace/nodejs$ sudo npm install express -g
  2. npm http GET https://registry.npmjs.org/express
  3.  
  4. npm ERR! Error: failed to fetch from registry: express
  5. npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
  6. npm ERR! at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
  7. npm ERR! at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
  8. npm ERR! at Request.callback (/usr/lib/nodejs/request/main.js:119:22)
  9. npm ERR! at Request. (/usr/lib/nodejs/request/main.js:212:58)
  10. npm ERR! at Request.emit (events.js:88:20)
  11. npm ERR! at ClientRequest. (/usr/lib/nodejs/request/main.js:412:12)
  12. npm ERR! at ClientRequest.emit (events.js:67:17)
  13. npm ERR! at HTTPParser.onIncoming (http.js:1261:11)
  14. npm ERR! at HTTPParser.onHeadersComplete (http.js:102:31)
  15. npm ERR! You may report this log at:
  16. npm ERR!
  17. npm ERR! or use
  18. npm ERR! reportbug --attach /home/vivianzhang/workspace/nodejs/npm-debug.log npm
  19. npm ERR!
  20. npm ERR! System Linux 3.11.0-15-generic
  21. npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
  22. npm ERR! cwd /home/vivianzhang/workspace/nodejs
  23. npm ERR! node -v v0.6.12
  24. npm ERR! npm -v 1.1.4
  25. npm ERR! message failed to fetch from registry: express
  26. npm ERR!
  27. npm ERR! Additional logging details can be found in:
  28. npm ERR! /home/vivianzhang/workspace/nodejs/npm-debug.log
  29. npm not ok
  30. vivianzhang@ubuntu:~/workspace/nodejs$

The solution for “Error: failed to fetch from registry: express” is run the following in your terminal window:

 
 
  1. vivianzhang@ubuntu:~/workspace/nodejs$ npm config set registry http://registry.npmjs.org/
  2. vivianzhang@ubuntu:~/workspace/nodejs$ sudo npm install express -gnpm http GET http://registry.npmjs.org/express
  3. npm http 200 http://registry.npmjs.org/express
  4. npm http GET http://registry.npmjs.org/express/-/express-3.4.4.tgz
  5. npm http 200 http://registry.npmjs.org/express/-/express-3.4.4.tgz
  6. npm http GET http://registry.npmjs.org/connect/2.11.0
  7. npm http GET http://registry.npmjs.org/commander/1.3.2
  8. npm http GET http://registry.npmjs.org/range-parser/0.0.4
  9. npm http GET http://registry.npmjs.org/mkdirp/0.3.5
  10. npm http GET http://registry.npmjs.org/cookie/0.1.0
  11. npm http GET http://registry.npmjs.org/buffer-crc32/0.2.1
  12. npm http GET http://registry.npmjs.org/fresh/0.2.0
  13. npm http GET http://registry.npmjs.org/cookie-signature/1.0.1
  14. npm http GET http://registry.npmjs.org/methods/0.1.0
  15. npm http GET http://registry.npmjs.org/send/0.1.4
  16. npm http GET http://registry.npmjs.org/debug
  17. npm http 200 http://registry.npmjs.org/cookie/0.1.0
  18. npm http GET http://registry.npmjs.org/cookie/-/cookie-0.1.0.tgz
  19. npm http 200 http://registry.npmjs.org/buffer-crc32/0.2.1
  20. npm http GET http://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.1.tgz
  21. npm http 200 http://registry.npmjs.org/commander/1.3.2
  22. npm http 200 http://registry.npmjs.org/connect/2.11.0
  23. npm http 200 http://registry.npmjs.org/mkdirp/0.3.5
  24. npm http GET http://registry.npmjs.org/commander/-/commander-1.3.2.tgz
  25. npm http GET http://registry.npmjs.org/connect/-/connect-2.11.0.tgz
  26. npm http GET http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
  27. npm http 200 http://registry.npmjs.org/range-parser/0.0.4
  28. npm http GET http://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
  29. npm http 200 http://registry.npmjs.org/fresh/0.2.0
  30. npm http GET http://registry.npmjs.org/fresh/-/fresh-0.2.0.tgz
  31. npm http 200 http://registry.npmjs.org/cookie/-/cookie-0.1.0.tgz
  32. npm http 200 http://registry.npmjs.org/send/0.1.4
  33. npm http 200 http://registry.npmjs.org/methods/0.1.0
  34. npm http 200 http://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.1.tgz
  35. npm http 200 http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
  36. npm http GET http://registry.npmjs.org/send/-/send-0.1.4.tgz
  37. npm http GET http://registry.npmjs.org/methods/-/methods-0.1.0.tgz
  38. npm http 200 http://registry.npmjs.org/debug
  39. npm http 200 http://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
  40. npm http GET http://registry.npmjs.org/debug/-/debug-0.7.4.tgz
  41. npm http 200 http://registry.npmjs.org/commander/-/commander-1.3.2.tgz
  42. npm http 200 http://registry.npmjs.org/connect/-/connect-2.11.0.tgz
  43. npm http 200 http://registry.npmjs.org/send/-/send-0.1.4.tgz
  44. npm http 200 http://registry.npmjs.org/fresh/-/fresh-0.2.0.tgz
  45. npm http 200 http://registry.npmjs.org/cookie-signature/1.0.1
  46. npm http 200 http://registry.npmjs.org/debug/-/debug-0.7.4.tgz
  47. npm http 200 http://registry.npmjs.org/methods/-/methods-0.1.0.tgz
  48. npm http GET http://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.1.tgz
  49. npm http 200 http://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.1.tgz
  50. npm ERR! error installing express@3.4.4
  51. npm ERR! error rolling back express@3.4.4 Error: UNKNOWN, unknown error '/usr/local/lib/node_modules/express'
  52.  
  53. npm ERR! Unsupported
  54. npm ERR! Not compatible with your version of node/npm: connect@2.11.0
  55. npm ERR! Required: {"node":">= 0.8.0"}
  56. npm ERR! Actual: {"npm":"1.1.4","node":"0.6.12"}
  57. npm ERR!
  58. npm ERR! System Linux 3.11.0-15-generic
  59. npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
  60. npm ERR! cwd /home/vivianzhang/workspace/nodejs
  61. npm ERR! node -v v0.6.12
  62. npm ERR! npm -v 1.1.4
  63. npm ERR! code ENOTSUP
  64. npm ERR! message Unsupported
  65. npm ERR! errno {}
  66. npm http GET http://registry.npmjs.org/mime
  67. npm http GET http://registry.npmjs.org/keypress
  68. npm ERR!
  69. npm ERR! Additional logging details can be found in:
  70. npm ERR! /home/vivianzhang/workspace/nodejs/npm-debug.log
  71. npm not ok

Prompt indicates that node and npm version are incompatible. As of this moment, both Conan and Vivian reached the same point of progress.

 

2. Download the source code from github install – successful

To fix errors Conan and Vivian got, we would find compatible version, and install them manually.

Uninstall the node and npm we got from last step

 
 
  1. ~ sudo apt-get autoremove npm
  2. ~ sudo apt-get autoremove nodejs

Find the node official repo: https://github.com/joyent/node

Install git for your ubuntu

 
 
  1. ~ sudo apt-get install git

install nodejs from github

 
 
  1. ~ git clone git://github.com/joyent/node.git
  2. Cloning into 'node'...
  3. remote: Counting objects: 100200, done.
  4. remote: Compressing objects: 100% (28074/28074), done.
  5. remote: Total 100200 (delta 78807), reused 90936 (delta 70473)
  6. Receiving objects: 100% (100200/100200), 61.81 MiB | 698 KiB/s, done.
  7. Resolving deltas: 100% (78807/78807), done.

Go inside of node directory

 
 
  1. ~ cd node
  2. ~ pwd
  3. /home/vivianzhang/workspace/nodejs/node

switch the new repo branch

 
 
  1. ~ git checkout v0.11.2-release
  2. Branch v0.11.2-release set up to track remote branch v0.11.2-release from origin.
  3. Switched to a new branch 'v0.11.2-release'

Install node

 
 
  1. ./configure
  2. make
  3. sudo make install

after installation is finished, check version of node

 
 
  1. ~ node -v
  2. -bash: /usr/bin/node: No such file or directory

We got prompt which states ” no node”, let us check the directory node was installed:

Conan got:

 
 
  1. ~ whereis node
  2. node: /usr/local/bin/node

Vivian got:

 
 
  1. ~ whereis node
  2. node:

If you got errors like Vivian did,  do the following:

Based on stackoverflow post, we can fix it by running:

 
 
  1. sudo apt-get purge nodejs npm

Then install from Chris Lea’s repo:

 
 
  1. sudo apt-get update
  2. sudo apt-get install -y python-software-properties python g++ make
  3. sudo add-apt-repository ppa:chris-lea/node.js
  4. sudo apt-get update
  5. sudo apt-get install nodejs

Run the command again, you should get the same output Conan got:

 
 
  1. ~ whereis node
  2. node: /usr/local/bin/node

As of this moment, both Conan and Vivian reached the same point of progress. Then do the following:

Add symlink for node

 
 
  1. ~ sudo ln -s /usr/local/bin/node /usr/bin/node
  2. ~ sudo ln -s /usr/local/bin/npm /usr/bin/npm

Then we check version of node and npm again

 
 
  1. ~ node -v
  2. v0.11.2
  3. ~ npm -v
  4. 1.2.21

We install express

 
 
  1. ~ sudo npm install express -g
  2. express@3.5.1 /usr/local/lib/node_modules/express
  3. ├── methods@0.1.0
  4. ├── debug@0.7.4
  5. ├── merge-descriptors@0.0.2
  6. ├── cookie-signature@1.0.3
  7. ├── fresh@0.2.2
  8. ├── range-parser@1.0.0
  9. ├── buffer-crc32@0.2.1
  10. ├── cookie@0.1.1
  11. ├── mkdirp@0.3.5
  12. ├── commander@1.3.2 (keypress@0.1.0)
  13. ├── send@0.2.0 (mime@1.2.11)
  14. └── connect@2.14.1 (response-time@1.0.0, pause@0.0.1, connect-timeout@1.0.0, method-override@1.0.0, vhost@1.0.0, static-favicon@1.0.0, qs@0.6.6, morgan@1.0.0, serve-static@1.0.2, basic-auth-connect@1.0.0, bytes@0.2.1, raw-body@1.1.3, errorhandler@1.0.0, cookie-parser@1.0.1, csurf@1.0.0, compression@1.0.0, express-session@1.0.2, serve-index@1.0.1, multiparty@2.2.0)vivianzhang@ubuntu:~/workspace/nodejs/node$

The installation is successful now.

 

3. Establish express project , start your first project

 
 
  1. ~ express -e nodejs-demo
  2. create : nodejs-demo
  3. create : nodejs-demo/package.json
  4. create : nodejs-demo/app.js
  5. create : nodejs-demo/public
  6. create : nodejs-demo/public/javascripts
  7. create : nodejs-demo/public/images
  8. create : nodejs-demo/public/stylesheets
  9. create : nodejs-demo/public/stylesheets/style.css
  10. create : nodejs-demo/routes
  11. create : nodejs-demo/routes/index.js
  12. create : nodejs-demo/routes/user.js
  13. create : nodejs-demo/views
  14. create : nodejs-demo/views/index.ejs
  15. install dependencies:
  16. $ cd nodejs-demo && npm install
  17. run the app:
  18. $ node app

Install the dependent packages

 
 
  1. ~ cd nodejs-demo
  2. ~ sudo npm install
  3. express@3.5.1 node_modules/express
  4. ├── methods@0.1.0
  5. ├── debug@0.7.4
  6. ├── merge-descriptors@0.0.2
  7. ├── cookie-signature@1.0.3
  8. ├── range-parser@1.0.0
  9. ├── fresh@0.2.2
  10. ├── buffer-crc32@0.2.1
  11. ├── cookie@0.1.1
  12. ├── mkdirp@0.3.5
  13. ├── send@0.2.0 (mime@1.2.11)
  14. ├── commander@1.3.2 (keypress@0.1.0)
  15. └── connect@2.14.1 (response-time@1.0.0, pause@0.0.1, connect-timeout@1.0.0, method-override@1.0.0, static-favicon@1.0.0, vhost@1.0.0, qs@0.6.6, basic-auth-connect@1.0.0, morgan@1.0.0, serve-static@1.0.2, bytes@0.2.1, raw-body@1.1.3, errorhandler@1.0.0, csurf@1.0.0, cookie-parser@1.0.1, compression@1.0.0, express-session@1.0.2, serve-index@1.0.1, multiparty@2.2.0)
  16. vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$

Start the app

 
 
  1. vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$ node app.js
  2. Express server listening on port 3000

Test whether we can call curl by opening another terminal window by ctrl+alt+T

 
 
  1. vivianzhang@ubuntu:~$ curl localhost:3000
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Express</title>
  6. <link rel='stylesheet' href='/stylesheets/style.css' />
  7. </head>
  8. <body>
  9. <h1>Express</h1>
  10. <Welcome to Express</p>
  11. <body>
  12. <html>

From previous terminal window, you will see Node.js server log

 
 
  1. vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$ node app.js
  2. Express server listening on port 3000
  3. GET / 200 14ms - 206b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值