define

1.0.1 • Public • Published

DefineJS

DefineJS is an Asynchronous Module Definition wrapper for Node. Writing modules in AMD style is convenient when code is shared between Node and a browser. The client needs to use an AMD loader like RequireJS.

Installation

Add a dependency to your project's package.json and run npm install:

{
    "dependencies" {
        "define": "1.0"
    }
}

or install manually:

npm install define

Get Started

In your app's main script:

require('define');

This will add define as a global so it's available everywhere else in the application automatically. Create AMD modules anywhere you like. They can be loaded with require or define.

See examples/shared-code for a working example web server.

Features

  • Share code with web browsers!
  • Very lightweight integration with Node's module system.
  • No need for conditional require('define') in every AMD module.
  • Compatible with AMD Define.
  • Compatible with AMD Require.

DefineJS is Global

DefineJS installs define as a global when it's loaded. The makes define available everywhere else in the application without any special workarounds in each file.

If you don't want a global definition, do the following in your app's main script:

require('define').noConflict();

then, for individual modules that need define:

if (typeof define === 'undefined') {
    var define = require('define');
}

However, if you prefer this approach and you're using RequireJS for the client-side loader anyway, you should probably just use the similar amdefine convention because the RequireJS optimizer strips it away.

Module Ids

The AMD Spec allows module definitions to optionally specify an id. This is very helpful for transport when several modules are concatenated into the same file.

DefineJS currently accepts module ids, but ignores them. This is because it's just a very light wrapper around Node's require. Node uses a module's absolute filename as the id, so defining more than one module in a file is currently unsupported.

In practice, this doesn't matter very much. Just follow the one-module-to-one-file convention and use something like the RequireJS Optimizer for client-side code in production.

See Also

Refer to the AMD Wiki for more information about AMD.

Readme

Keywords

none

Package Sidebar

Install

npm i define

Weekly Downloads

165

Version

1.0.1

License

none

Last publish

Collaborators

  • weaver