Node.js Interop for Dart

Dart Node.js Release License Coverage Build

This package provides Node.js interoperability for Dart.

Requirements

If you plan to play with the sources, you will also need the latest Grinder version.

Installing via Pub

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  nodejs_interop: *

2. Install it

Install this package and its dependencies from a command prompt:

$ pub get

3. Import it

Now in your Dart code, you can use:

import 'dart:js';
import 'package:nodejs_interop/nodejs_interop.dart';

Usage

The globals module

It provides objects that are available globally:

print(dirname); // "/path/to"
print(filename); // "app.dart.js"
print(module.id); // "/path/to/app.dart.js"

print(process.arch); // "x64"
print(process.platform); // "linux"

The assert module

This module provides a simple set of assertion tests that can be used to test invariants:

AssertModule _assert = require('assert');

_assert.ok(true); // Returns normally.
_assert.ok(false); // Throws a `JSAssertionError`.

The events module

This module provides the EventEmitter class:

// Required: the `EventEmitter` class doesn't exist before this call.
loadLibrary('events');

var listener = allowInterop((data) => print('A "eventName" event occurred: ${data['foo']}'));
var emitter = new EventEmitter()..on('eventName', listener);
emitter.emit('eventName', new JsObject.jsify({'foo': 'bar'}));

The os module

This module provides a number of operating system-related utility methods:

OSModule os = require('os');

print(os.homedir()); // "/home/user"
print(os.tmpdir()); // "/tmp"

The path module

This module provides utilities for working with file and directory paths:

PathModule path = require('path');

print(path.basename('/foo/bar/baz.txt')); // "baz.txt"
print(path.dirname('/foo/bar/baz.txt')); // "/foo/bar"

Unit tests

In order to run the tests, you must install the Node.js test dependencies by running the following command:

$ NODE_ENV=development npm install

Then, you must tell the test runtime where are located the newly installed npm packages by setting the NODE_PATH environment variable to the path of the local node_modules folder:

$ export NODE_PATH="$PWD/node_modules"

Finally, you can launch the test runner from the command prompt:

$ pub run test

See also

License

Node.js Interop for Dart is distributed under the MIT License.

Libraries

nodejs_interop
Provides interoperability with Node.js.
nodejs_interop.io
Provides file, socket, HTTP, and other I/O support for Node.js applications.
nodejs_interop.js
Provides interoperability with JavaScript APIs.
nodejs_interop.node
Provides interoperability with Node.js APIs.
nodejs_interop.npm
Provides interoperability with a predefined set of npm packages.