Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 54 lines (28 sloc) 3.067 kb
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
1 Title: Creating Custom Modules
2 Author: Aaron Blohowiak
3 Date: Tue May 18 2010 13:37:07 GMT-0500 (CDT)
e4dc85c @creationix Update custom modules article.
authored
4 Node: v0.1.102
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
5
6 Node implements [CommonJS Modules 1.0](http://commonjs.org/specs/modules/1.0/). Node's [full API doc.](http://nodejs.org/api.html#modules-285) explains their use thoroughly, but can be a bit tricky to get started.
7
8 This tutorial explains the why and how you can use Node's module system to help structure your application.
9
10 ## Creating a Module
11
12 A module is just a javascript file. Put some JS in a file and BAM! you have a module. Good show!
13
14 <creating-custom-modules/simplest-module-ever.js>
15
16 ## Um, so why is there a tutorial?
17
18 Unfortunately, you won't be able to do anything with it just yet.
19
20 <creating-custom-modules/simplest-module-ever-test.js>
21
22 There's a good reason, and it has to do with playing well with others. One of the biggest problems in JavaScript is name collisions. Here's an example: You get one script that makes a cool date picker and another that does a day-by-day schedule and they both define a drawCalendar() function and now it all breaks and how the heck are you going to fix it? This happens because both names are declared in what's called the global name space.
23
24 You know how I said that a module is just a javascript file? Well, that's true, but it is evaluated in a special way. When Node loads your javascript file it creates a new scope, so your date picker plugin can't mess with my schedule plugin. When you are in your module, you can't see the outside world; you can just do your own thing or require other modules. As a result, you don't have to worry about clashing with other people's stuff.
25
26 ## If you can't add to the global name space, then how do you share your code?
27
28 Inside your javascript file, there is a special object. When Node creates the new context for your module, it sprinkles some objects in there like a little salt and pepper. The [official Global Objects API](http://nodejs.org/api.html#global-objects-40) hints at the answer, but seeing is much simpler.
29
30 <creating-custom-modules/simplest-module-ever-complete.js>
31
32 And now this will work the way we want.
33
34 <creating-custom-modules/simplest-module-ever-complete-test.js>
35
36
37 ## Using Parts of Modules
38
39 The basic example on the [Node.JS Home Page](http://nodejs.org) shows you how to import whole modules.
40
41 When you want to use a port of a Module you use the require function like usual, but only save a reference to a member of the returned exports object:
42
e4dc85c @creationix Update custom modules article.
authored
43 var inspect = require('sys').inspect;
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
44
e4dc85c @creationix Update custom modules article.
authored
45 This creates a local variable, 'inspect' and assigns it to the 'inspect' property of the sys module.
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
46
47 So now you can
48
e4dc85c @creationix Update custom modules article.
authored
49 inspect({Hello:"World"})
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
50
e4dc85c @creationix Update custom modules article.
authored
51 which is like `sys.inspect({Hello:"World"})` without having to type `sys.inspect` all the time.
65f8749 @aaronblohowiak Adding creating-custom-modules article and my author file
aaronblohowiak authored
52
53 Normal JavaScript function binding rules apply, so if you have any weirdness that you can't figure out, just import the whole module and use it like usual until you [Learn more about JavaScript Binding](http://www.robertsosinski.com/2009/04/28/binding-scope-in-javascript/)
Something went wrong with that request. Please try again.