Build Nodejs development environment process sharing in Ubuntu

  • 2020-03-30 03:08:30
  • OfStack

System environment:

64 - bit Ubuntu 12.04 LTS

A,   Installation of nodejs by apt-get failed (you can see the source installation method below) :


~ sudo apt-get install nodejs
~ sudo apt-get install npm
~ node -v
v0.6.12
~ npm -v
1.1.4

Create a working directory

~ mkdir workspace
~ mkdir workspace/nodejs
~ cd workspace/nodejs
~ pwd
/home/conan/workspace/nodejs

Installation failed

sudo npm install express -g
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/connect/2.7.11
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.4
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/send/0.1.0
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/debug
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/connect/2.7.11
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
npm http 304 https://registry.npmjs.org/cookie/0.1.0
npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
npm http 304 https://registry.npmjs.org/debug
npm ERR! error installing express@3.2.6
npm ERR! error rolling back express@3.2.6 Error: UNKNOWN, unknown error '/usr/local/lib/node_modules/express'
npm ERR! Unsupported
npm ERR! Not compatible with your version of node/npm: connect@2.7.11
npm ERR! Required: {"node":">= 0.8.0"}
npm ERR! Actual: {"npm":"1.1.4","node":"0.6.12"}
npm ERR!
npm ERR! System Linux 3.5.0-23-generic
npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
npm ERR! cwd /home/conan/workspace/nodejs
npm ERR! node -v v0.6.12
npm ERR! npm -v 1.1.4
npm ERR! code ENOTSUP
npm ERR! message Unsupported
npm ERR! errno {}
npm http GET https://registry.npmjs.org/mime/1.2.6
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/conan/workspace/nodejs/npm-debug.log
npm not ok

Node and NPM versions are not compatible. It may be that the apt-get source has not been updated.

Download the source code from github and install it successfully

To find a compatible version, install it manually.
Uninstall the node and NPM you just installed


~ sudo apt-get autoremove npm
~ sudo apt-get autoremove nodejs
 find nodejs Official release download: https://github.com/joyent/node


In ubuntu, install git first

~ sudo apt-get install git

Next, download the nodejs source code from github
~ git clone git://github.com/joyent/node.git
Cloning into 'node'...
remote: Counting objects: 100200, done.
remote: Compressing objects: 100% (28074/28074), done.
remote: Total 100200 (delta 78807), reused 90936 (delta 70473)
Receiving objects: 100% (100200/100200), 61.81 MiB | 698 KiB/s, done.
Resolving deltas: 100% (78807/78807), done.

Go to the node directory
~ cd node
~ pwd
/home/conan/workspace/nodejs/node

Switch to the latest release version v0.11.2-release
~ git checkout v0.11.2-release
Branch v0.11.2-release set up to track remote branch v0.11.2-release from origin.
Switched to a new branch 'v0.11.2-release'

To install
./configure
make
sudo make install

The installation is complete and view the node version
~ node -v
-bash: /usr/bin/node: No such file or directory

Error, no node found, check the node installation location
~ whereis node
node: /usr/local/bin/node

Add soft links: node and NPM to /usr/bin
~ sudo ln -s /usr/local/bin/node /usr/bin/node
~ sudo ln -s /usr/local/bin/npm /usr/bin/npm

Let's look at the node and NPM versions
~ node -v
v0.11.2
~ npm -v
1.2.21

Now install express

~ sudo npm install express -g
express@3.2.6 /usr/local/lib/node_modules/express
 ├ ─ ─  methods@0.0.1
 ├ ─ ─  fresh@0.1.0
 ├ ─ ─  range-parser@0.0.4
 ├ ─ ─  cookie-signature@1.0.1
 ├ ─ ─  buffer-crc32@0.2.1
 ├ ─ ─  cookie@0.1.0
 ├ ─ ─  debug@0.7.2
 ├ ─ ─  commander@0.6.1
 ├ ─ ─  mkdirp@0.3.4
 ├ ─ ─  send@0.1.0 (mime@1.2.6)
 └ ─ ─  connect@2.7.11 (pause@0.0.1, qs@0.6.5, bytes@0.2.0, cookie@0.0.5, formidable@1.0.14, send@0.1.1)

Successful installation.

Iii. Establish express project and start the first project


~ express -e nodejs-demo
create : nodejs-demo
create : nodejs-demo/package.json
create : nodejs-demo/app.js
create : nodejs-demo/public
create : nodejs-demo/public/javascripts
create : nodejs-demo/public/images
create : nodejs-demo/public/stylesheets
create : nodejs-demo/public/stylesheets/style.css
create : nodejs-demo/routes
create : nodejs-demo/routes/index.js
create : nodejs-demo/routes/user.js
create : nodejs-demo/views
create : nodejs-demo/views/index.ejs
install dependencies:
$ cd nodejs-demo && npm install
run the app:
$ node app

Install dependency package

~ cd nodejs-demo
~ sudo npm install
express@3.2.6 node_modules/express
 ├ ─ ─  methods@0.0.1
 ├ ─ ─  fresh@0.1.0
 ├ ─ ─  range-parser@0.0.4
 ├ ─ ─  cookie-signature@1.0.1
 ├ ─ ─  buffer-crc32@0.2.1
 ├ ─ ─  cookie@0.1.0
 ├ ─ ─  debug@0.7.2
 ├ ─ ─  commander@0.6.1
 ├ ─ ─  mkdirp@0.3.4
 ├ ─ ─  send@0.1.0 (mime@1.2.6)
 └ ─ ─  connect@2.7.11 (pause@0.0.1, qs@0.6.5, bytes@0.2.0, cookie@0.0.5, formidable@1.0.14, send@0.1.1)

Start the program

~ node app.js
Express server listening on port 3000

Test whether curl starts successfully


~ sudo apt-get install curl
~ curl localhost:3000
<!DOCTYPE html>
<html>
<head>
<title>Express</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Express</h1>
<p>Welcome to Express</p>
</body>
</html>

Nodejs server log:

GET / 200 6ms - 206b

Ok, so we've successfully set up a nodejs development environment in ubuntu. Now you can enjoy the fun of development.


Related articles: