You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<liclass="hbox"><ahref="#cjsmodule">Define a Module with Simplified CommonJS Wrapper</a><spanclass="spacer boxFlex"></span><spanclass="sect">§ 1.2.5</span></li>
15
+
<liclass="hbox"><ahref="#modulename">Define a Module with a name</a><spanclass="spacer boxFlex"></span><spanclass="sect">§ 1.2.6</span></li>
<liclass="hbox"><ahref="#i18n">Define an I18N Bundle</a><spanclass="spacer boxFlex"></span><spanclass="sect">§ 1.3</span></li>
19
20
<liclass="hbox"><ahref="#text">Specify a Text File Dependency</a><spanclass="spacer boxFlex"></span><spanclass="sect">§ 1.4</span></li>
@@ -76,7 +77,15 @@ <h3>
76
77
<li>b.js (in the same directory as the HTML page that has the above HTML snippet)</li>
77
78
</ul>
78
79
79
-
<p>Files that end in ".js" are assumed to just be plain JS files that do not use the RequireJS module syntax, and therefore do not use the module-to-path algorithm used for looking up dot-notation modules, like "some/module" above.</p>
80
+
<p>RequireJS normally uses the <ahref="#config-baseUrl">baseUrl</a> and <ahref="#config-paths">paths</a> configuration to convert names like "some/module" into a file path.</p>
81
+
82
+
<p>However, if the dependency name has one of the following properties, it is treated as a regular file path, like something that was passed to a <script src=""> tag:</p>
83
+
84
+
<ul>
85
+
<li>Ends in ".js".</li>
86
+
<li>Starts with a "/".</li>
87
+
<li>Contains an URL protocol, like "http:" or "https:".</li>
88
+
</ul>
80
89
81
90
<p>See the <ahref="#config">Configuration Options</a> section for information on changing the lookup paths used for dependencies.</p>
82
91
@@ -218,8 +227,20 @@ <h4><a name="funcmodule">Define a Module as a Function</a><span class="sectionMa
218
227
</code></pre>
219
228
</div>
220
229
230
+
231
+
<divclass="subSection">
232
+
<h4><aname="cjsmodule">Define a Module with Simplified CommonJS Wrapper</a><spanclass="sectionMark">§ 1.2.5</span></h4>
233
+
234
+
<p>If you wish to reuse some code that was written in the traditional <ahref="http://wiki.commonjs.org/wiki/Modules/1.1.1">CommonJS module format</a> it may be difficult to re-work to the array of dependencies used above. Also, Node 0.5 will likely support a limited kind of define() function that provides a simple wrapper over CommonJS files. So if you are building a small utility and you wish it to be used in both Node (without using the <ahref="node.html">RequireJS Node adapter</a>), and in the browser, you can use the <ahref="commonjs.html">simplified CommonJS wrapper</a>.</p>
235
+
236
+
<p>It can also be useful if you have many dependencies, and keeping dependency array positioning in sync with the function argument positioning is undesirable.</p>
237
+
238
+
<p>This wrapper relies on Function.prototype.toString() to give a useful string value of the function contents. This does not work on some devices like the PS3 and some older Opera mobile browsers. Use the <ahref="optimization.html">optimizer</a> to pull out the dependencies in the array format for use on those devices.</p>
239
+
</div>
240
+
241
+
221
242
<divclass="subSection">
222
-
<h4><aname="modulename">Define a Module with a Name</a><spanclass="sectionMark">§ 1.2.5</span></h4>
243
+
<h4><aname="modulename">Define a Module with a Name</a><spanclass="sectionMark">§ 1.2.6</span></h4>
223
244
224
245
<p>You may encounter some define() calls that include a name for the module as the first argument to define():</p>
225
246
@@ -237,7 +258,7 @@ <h4><a name="modulename">Define a Module with a Name</a><span class="sectionMark
<p><strong>One module per file.</strong>: Only one module should be defined per JavaScript file, given the nature of the module name-to-file-path lookup algorithm. Multiple modules will be grouped into optimized files by the <ahref="optimization.html">optimization tool</a>, but you should only use the optimization tool to place more than one module in a file.</p>
<p>If you define a circular dependency (A needs B and B needs A), then in this case when B's module function is called, it will get an undefined value for A. B can fetch A later after modules have been defined by using the require() method (be sure to specify require as a dependency so the right context is used to look up A):</p>
<p>Also seen if you manually code a script tag in HTML to load a script that has a few named modules, but then try to load an anonymous module that ends up having the same name as one of the named modules in the script loaded by the manually coded script tag.</p>
22
22
23
-
<p>Finally, if you use the loader plugins or anonymous modules but do not use the RequireJS optimizer to combine files together, this error can occur. The optimizer knows how to name anonymous modules correctly so that can be combined with other modules in an optimized file.</p>
23
+
<p>Finally, if you use the loader plugins or anonymous modules (modules that call define() with no string ID) but do not use the RequireJS optimizer to combine files together, this error can occur. The optimizer knows how to name anonymous modules correctly so that they can be combined with other modules in an optimized file.</p>
Copy file name to clipboardExpand all lines: docs/node.html
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,8 @@ <h2>
35
35
36
36
<p>RequireJS will use its <ahref="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 <ahref="http://npmjs.org/">npm</a>, and do not configure their locations with RequireJS.</p>
37
37
38
+
<p><strong>Best practice</strong>: Use npm to install Node-only packages/modules into the projects <strong>node_modules</strong> directory, but do not configure RequireJS to look inside the node_modules directory. Also avoid using relative module IDs to reference modules that are Node-only modules. So, <strong>do not</strong> do something like <strong>require("./node_modules/foo/foo")</strong>.</p>
39
+
38
40
<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>
39
41
40
42
<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>
0 commit comments