node_interop 1.0.0-dev.2.0 node_interop: ^1.0.0-dev.2.0 copied to clipboard
Node.js API bindings and utilities for Dart.
Node Interop #
Write applications in Dart, run in NodeJS.
What is this? #
This library provides JavaScript bindings and some utilities to work with core Node APIs and built-in modules.
To compile Dart applications as Node modules see build_node_compilers package.
For a more Dart-like experience with Node I/O system see node_io package which is designed as a drop-in replacement for
dart:io
.For a Dart-style HTTP client checkout node_http.
Example #
Here is an example Node app written in Dart:
import 'package:node_interop/node.dart';
void main() {
print("Hello world, I'm currently in ${process.cwd()}.");
}
This application can be compiled with build_node_compilers and executed in Node.
For more examples using different APIs see example/
folder.
Structure #
For each built-in Node module there is a separate Dart file in the lib/
folder. So to access Node's os
module, for instance, you'd need to use
following import:
import 'package:node_interop/os.dart';
Note that after importing a module like above there is no need to also require
it (the Node way). Each library file (like os.dart
) exposes library-level
property of the same name which gives you access to that module's functionality.
This is just a convenience to not have to import
and require
modules at
the same time. Here is how os.dart
implements it:
// file:lib/os.dart
@JS()
library node_interop.os;
import 'package:js/js.dart';
import 'node.dart';
OS get os => require('os');
@JS()
@anonymous
abstract class OS {
external List<CPU> cpus();
// ...
}
Not all built-in Node modules need to be required, like buffer
module for
instance. They still have a dedicated Dart file in this library, but this is
mostly for consistency and you shouldn't need to import it directly. The
buffer
module is globally available in Node.
Libraries with underscores in their name (like child_process
) expose
library-level property with underscores converted to camelCase to be compliant
with Dart code style rules:
import 'package:node_interop/child_process.dart';
void main() {
childProcess.execSync('ls -la');
}
Status #
While 1.0.0 is still in dev
mode breaking changes are likely to occur.
Make sure to checkout CHANGELOG.md after every release, all notable changes and upgrade instructions will be described there.
If you found a bug, please don't hesitate to create an issue in the issue tracker.
Features and bugs #
Please file feature requests and bugs at the issue tracker.