#

Jump to Table of Contents

# YUI on Node.js

As of 3.5.0, YUI runs natively on Node.js and comes with an official npm package for easy installation. YUI supports Node.js > 0.4.10, but should work on stable releases above that. Unstable releases will be supported in local developer builds as features change.

## About YUI on Node.js

The core library will run on Node.js with minimal external dependencies. `Get.script`, `JSONP`, `YQL` & `io` along with our core infrastructure pieces (anything that doesn't require a DOM) will work out of the box. The only dependency required to run YUI inside Node.js is Mikeal Roger's Request library. It's used under the hood for `Y.Get` & `Y.IO`.

However, there is no DOM support included in our releases. This means that you can not do DOM manipulation on the server with this core system. With the introduction of `Y.Handlebars`, we are working very hard to allow Widget rendering on the server via strings instead of raw DOM manipulation. If you need DOM support, please look into our YUI, Node.js and DOM example.

## Install via npm

Installing YUI with npm is easy and fast. Simply use `npm` to install the latest stable, official `yui` package:

    npm install yui@stable

This will install the latest YUI release into your local `./node_modules` directory to require in your Node.js application.

## Using YUI on Node.js

Using YUI on Node.js is very similar to using it inside the browser:

    var YUI = require('yui').YUI;

    YUI().use('yql', function(Y) {
        Y.YQL('...');
    });

When you `require()` the `yui` module in this fashion, you are returned the global `YUI` object. It will be re-used on each additional `require()`. So if you load modules into one instance, then re-require YUI, those modules will still be available without fetching them from disk again.

Note: Since the same global `YUI` object is used within the Node.js process, all calls to all YUI instances' `use()` methods share the same module loading queue. You should be aware of this when using multiple YUI instances configured with different `useSync` values.

## Requiring a YUI module

If you are working on a package-able application, you can also use YUI in a sync mode to allow for exporting a YUI module or a custom module that requires a YUI module.

For example, here we are only requiring the `YQL` module and the return is now a `YUI` instance with the `YQL` module already attached to it:

    var Y = require('yui/yql');

This way you can export a custom module like this:

    #!/usr/bin/env node

    var Y = require('yui/yql');

    module.exports.getYQLData = function(cb) {
        Y.YQL('select * from awesome.datasource', function(r) {
            cb(r);
        });
    };

Now, your implementor can use your module like this:

    var mod = require('your-module');

    mod.getYQLData(function(r) {
        // `r` will be the return from the above YQL call.
    });

There are also other ways to require a single module, or a set of modules.

### One at a time

In the example below, you can see that the first require returns the `YQL` module, but doesn't return `Base`. However, the second require asking for `Base` returns the same instance as before with both the `YQL` and `Base` modules attached.

    #!/usr/bin/env node

    var Y = require('yui/yql');
    console.log('YQL #1?', Y.YQL ? true : false);   // => YQL #1? true
    console.log('Base #1?', Y.Base ? true : false); // => Base #1? false

    var Y = require('yui/base-base');
    console.log('YQL #2?', Y.YQL ? true : false);   // => YQL #2? true
    console.log('Base #2?', Y.Base ? true : false); // => Base #2? true

### In a batch

In this example, we have multiple modules being required from the object being returned from the initial require call.

    #!/usr/bin/env node

    var Y = require('yui').use('yql', 'oop', 'base-base');

    console.log('OOP?', Y.rbind ? true : false); // => OOP? true
    console.log('YQL?', Y.YQL ? true : false);   // => YQL? true
    console.log('Base?', Y.Base ? true : false); // => Base? true

The above is exactly the same as this:

    #!/usr/bin/env node

    var YUI = require('yui').YUI;

    var Y = YUI({ useSync: true }).use('yql', 'oop', 'base-base');

This approach makes it easy for you to create custom YUI modules and export them from inside your own app.

    var path = require('path'),
        YUI = require('yui').YUI;

    var Y = YUI({
        useSync: true,
        modules: {
            awesome: {
                fullpath: path.join(__dirname, './lib/awesome.js'),
                requires: [ 'yql', 'oop', 'base-base' ]
            }
        }
    }).use('awesome');

    // If your module adds a `Y.Awesome` namespace.
    module.exports.awesome = Y.Awesome;

## Using Debug Versions

Just like in the browser, YUI will load minimized versions of the requested modules. You can choose to load either all debug files, or single modules.

### Load all modules in debug mode

This will load the debug seed and force all modules to load in debug mode:

    #!/usr/bin/env node

    var YUI = require('yui/debug').YUI;

    YUI().use('base', fn);

### Debugging some modules

You may only want to debug a single module, you can do this by adding `/debug` to the original require:

    #!/usr/bin/env node

    var Y = require('yui/yql/debug');
    console.log('YQL?', (Y.YQL) ? true : false);

    Y.YQL('select * from weather.forecast where location=90210', function(r) {
        console.log(r.query.results.channel.item.description);
    });