Wrap your code in a define().
Let's start with define(). The define function announces to the AMD environment that you wish to declare a module. The signature of this function is pretty flexible, but let's start by focusing on the most common usage.
As you can see from the first parameter, you can pass an array of ids into define. These are the ids of other modules that your module depends upon to do its work. The second parameter is a function that creates your module and will be run exactly once. The factory is called with the dependent modules as parameters. Furthermore, it is guaranteed to run only after all of the dependent modules are known to be available. In most systems, the factory runs just before it's needed.
Our module, "app/mime-client", relies on two other modules, "rest" and "rest/interceptor/mime". The two required modules are mapped onto the factory's parameter list as rest and mime. You may name these however you wish, of course.
Here's the previous example as AMD-wrapped CommonJS.
Notice that the factory receives up to three arguments that emulate the CommonJS require, exports, and module variables.
In CommonJS, dependencies are assigned to local variables using require(id). This convention is known as the free require (or less commonly, the scoped require). In AMD it is known as the local require.
You should export your module, rather than return it. You may export in one of two ways. The simplest way, shown above, is to assign the module directly to module.exports. Note: module.exports = is not strictly CommonJS. However, it is a node.js extension to CommonJS that is widely supported.
The second way is to set properties on the exports object. There are use cases for each export mechanism, but that's a great topic for an upcoming tutorial.
There are many other variations of define parameters and even variations of the AMD-wrapped CommonJS format -- too many to cover in a simple tutorial. However, as you're browsing other cujo.js tutorials, there's one more variation you'll see a lot. We recommend the following variation when declaring wire.js "spec" modules.
As you can see, the factoryFunction parameter does not always have to be a function! When the exported value is static, as in the case above, the function wrapper may be omitted. The AMD environment detects non-functions in the last position and automatically wraps them in a factory function. Also, because this module does not have explicit dependencies, we can skip the dependency array, too.
For further reading on AMD modules, visit https://github.com/amdjs/amdjs-api/wiki/.