HowTo: Install node.js on Mac Snow Leopard with Express
by floriankubis
This is the first part of some basic howto on using node.js on Snow Leopard.
At the end of this Tutorial you will have an Expressjs App running on your Mac with the famous “hello world”.
Prerequisites:
Installed XCode (it’s also on your install DVD)
Installed Macports
Now open Terminal and go through the next steps:
1. First install node.js through macports:
$ sudo port selfupdate
$ sudo port install nodejs
2. Check install:
$ node -v
v0.2.2
3. Install Node Package Manager
3.1 Preparing your system
If using an OSX Admin user (most of you are) you are member of the admin user group.
The admin group owns /opt/local which is used by macports.
To install npm without sudo, which is highly recommended, your user must have write rights on the install folder:
$ sudo chmod -R g+w /opt/local/
Password:
3.2 Automatic installation of npm
You can use the default npm install methode:
$ curl http://npmjs.org/install.sh | sh
You should see this output:
node cli.js cache clean
npm info it worked if it ends with ok
npm info using npm@0.2.3-3
npm ok
node cli.js rm npm
npm info it worked if it ends with ok
npm info using npm@0.2.3-3
npm info uninstall safe to uninstall: npm@0.2.3-3
npm info uninstall npm@0.2.3-3 complete
npm ok
node cli.js install npm
npm info it worked if it ends with ok
npm info using npm@0.2.3-3
npm info fetch http://registry.npmjs.org/npm/-/npm@0.2.3-3.tgz
npm info install npm@0.2.3-3
npm info activate npm@0.2.3-3
npm info build Success: npm@0.2.3-3
npm ok
It worked
4. Check npm installation:
$ npm -v
0.2.3-3
5. Install expressjs using npm
$ npm install express
npm info it worked if it ends with ok
npm info using npm@0.2.3-3
npm info fetch http://registry.npmjs.org/express/-/express-1.0.0rc3.tgz
npm info fetch http://registry.npmjs.org/connect/-/connect-0.2.5.tgz
npm info install express@1.0.0rc3
npm info install connect@0.2.5
npm info activate express@1.0.0rc3
npm info activate connect@0.2.5
npm info build Success: express@1.0.0rc3
npm info build Success: connect@0.2.5
npm ok
6. Now it’s time for your reward:
6.1 Create a folder for your test:
$ mkdir -p ~/nodes/express_reward/
$ cd ~/nodes/express_reward/
6.2 Create your first application
Create a file called app.js and insert the code from the expressjs homepage.
If you have installed Textmate on your system:
$ mate app.js
Insert into file:
var app = require('express').createServer();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
6.3 Save your file
6.4 Back in Terminal start your app:
$ node app.js
7. Finished. You got node.js running and built your “Hello World” Express app
Now open: http://localhost:3000/ in browser:
Have fun with node.js and built your first express application.
Find more info at:
- http://nodejs.org/api.html
- http://github.com/isaacs/npm
- http://expressjs.com/guide.html
Comments
Hi,
I’m getting this error:
slab:express_reward b.irvin$ node app.js
node.js:63
throw e;
^
ReferenceError: express is not defined
at Object. (/Users/b.irvin/nodes/express_reward/app.js:1:63)
at Module._compile (node.js:462:23)
at Module._loadScriptSync (node.js:469:10)
at Module.loadSync (node.js:338:12)
at Object.runMain (node.js:522:24)
at Array. (node.js:756:12)
at EventEmitter._tickCallback (node.js:55:22)
at node.js:773:9
Hey your example throws an error, when running app.js
$ node app.js
node.js:116
throw e; // process.nextTick error, or ‘error’ event on first tick
^
TypeError: Cannot read property ‘prototype’ of undefined
at Object. (/opt/local/lib/node/.npm/express/1.0.7/package/lib/express/server.js:87:44)
at Module._compile (module.js:374:26)
at Object..js (module.js:380:10)
at Module.load (module.js:306:31)
at Function._load (module.js:272:10)
at require (module.js:318:19)
at Object. (/opt/local/lib/node/.npm/express/1.0.7/package/lib/express/index.js:28:31)
at Module._compile (module.js:374:26)
at Object..js (module.js:380:10)
at Module.load (module.js:306:31)
+1 to what bixu and JOsH reported, above.
Hi!
WordPress messes with the quotes. I fixed them in the articel. The howto works with the latest versions of node and npm (23. May 2011).
[...] 注意:后面是| sudo sh,网上一般的示例都是:| sh,那样子经常会出现权限错误,你也可以将相关文档改为可写的权限,参考这篇文章 [...]
npm install mime
npm install qs
npm install connect
npm install express
It’s worked for Lion.
Bummer…
stefans-mac:express_reward stefancross$ npm ls
/Users/stefancross
├─┬ connect@1.8.5
│ ├── formidable@1.0.8
│ └── qs@0.4.1
├─┬ express@2.5.6
│ ├── mkdirp@0.0.7
│ └── qs@0.4.1
└── mime@1.2.4
stefans-mac:express_reward stefancross$ node app.js
node.js:201
throw e; // process.nextTick error, or ‘error’ event on first tick
^
ReferenceError: e is not defined
at Object. (/Users/stefancross/nodes/express_reward/app.js:7:18)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
Any advice?
Opps, my syntax was a bit skewy when i copied and pasted, but after another look i still seem to be getting something wrong..
node app.js
node.js:201
throw e; // process.nextTick error, or ‘error’ event on first tick
^
Error: listen EADDRINUSE
at errnoException (net.js:616:11)
at Array.0 (net.js:717:26)
at EventEmitter._tickCallback (node.js:192:40)
Ah har! Gotchya,
Turns out i had WEBrick, the Rails lightweight built in server running on port 3000, moved it over to 8000 and ‘bobs your uncle’!
See here for more info http://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server
Also, sorry for spamming you old chap, just figured it might be of use for others