forked from requirejs/requirejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.html
More file actions
66 lines (46 loc) · 4.08 KB
/
node.html
File metadata and controls
66 lines (46 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<div id="directory" class="section">
<h1>RequireJS in Node</h1>
<ul class="index mono">
<li class="hbox">
<a href="#1">Doesn't Node already have a module loader?</a><span class="spacer boxFlex"></span><span class="sect">§ 1</span>
</li>
<li class="hbox">
<a href="#2">Can I use server modules written in the CommonJS module format?</a><span class="spacer boxFlex"></span><span class="sect">§ 2</span>
</li>
<li class="hbox">
<a href="#3">How do I use it?</a><span class="spacer boxFlex"></span><span class="sect">§ 3</span>
</li>
</ul>
</div>
<div class="section">
<h2>
<a name="1">Doesn't Node already have a module loader?</a>
<span class="sectionMark">§ 1</span>
</h2>
<p>Yes <a href="http://nodejs.org">Node</a> does. That loader uses the CommonJS module format. The CommonJS module format is <a href="why.html">non-optimal for the browser</a>, and I do not agree with <a href="http://tagneto.blogspot.com/2010/03/commonjs-module-trade-offs.html">some of the trade-offs made in the CommonJS module format</a>. By using RequireJS on the server, you can use one format for all your modules, whether they are running server side or in the browser. That way you can preserve the speed benefits and easy debugging you get with RequireJS in the browser, and not have to worry about extra translation costs for moving between two formats.</p>
</div>
<div class="section">
<h2>
<a name="2">Can I use Node modules already written in the CommonJS module format?</a>
<span class="sectionMark">§ 2</span></h2>
<p>Yes! The Node adapter for RequireJS, called r.js, will use Node's implementation of require and Node's search paths if the module is not found with the configuration used by RequireJS, so you can continue to use your existing Node-based modules without having to do changes to them.</p>
<p>However, the top most app file, the one you pass to r.js on startup, needs to be coded to the RequireJS API.</p>
<p>RequireJS will use its <a href="api.html#config">Configuration Options</a> first to find modules. If RequireJS cannot find the module with its configuration, it is assumed to be a module that uses Node's type of modules and configuration. So, only configure module locations with RequireJS if they use the RequireJS API. For modules that expect Node's APIs and configuration/paths, just install them with a Node package manager, like <a href="http://npmjs.org/">npm</a>, and do not configure their locations with RequireJS.</p>
<p>Even though RequireJS is an asynchronous loader in the browser, the RequireJS Node adapter loads modules synchronously in the Node environment to match the default loading behavior in Node.</p>
<p>Finally, RequireJS in Node can only load modules that are on the local disk -- fetching modules across http, for instance, is not supported at this time.</p>
</div>
<div class="section">
<h2>
<a name="3">How do I use it?</a>
<span class="sectionMark">§ 3</span>
</h2>
<p>Download r.js from the <a href="download.html#rjs">the download page</a> and place it on your disk somewhere. Then run this command:</p>
<pre><code>node path/to/r.js myNodeApp.js
</code></pre>
<p>This assumes you are in the directory that contains myNodeApp.js, your top-level Node application file. It also assumes myNodeApp.js uses the RequireJS API to load modules, and not Node's require('') syntax.</p>
<p><strong>Note:</strong> if you will be using npm-installed modules, it is best to place r.js as a sibling to myNodeApp.js, since Node will use the directory of r.js as the place to look up node_modules.</p>
<p>That is it!</p>
<p>If you want to try a sample app that works with r.js, try <a href="https://github.com/jrburke/r.js/blob/master/tests/node/index.js">the index.js Hello World app</a>.</p>
<p>If you find you have a problem, and want to report it, use the <a href="http://github.com/jrburke/requirejs/issues">RequireJS GitHub Issues page</a>.</p>
<p>If you want to discuss the RequireJS-Node integration, you can use the <a href="http://groups.google.com/group/requirejs">RequireJS Group</a>.</p>
</div>