Making a Dart file self-executable


The normal way to run a Dart file without first compiling it is through the dart run command. For example, if we have a bin/server.dart file, we can run it with:

dart run bin/server.dart

This works of course, but you can also make the bin/server.dart file self-executable by adding a shebang as the first line, and marking it as executable. So:

#!/usr/bin/env dart
import 'dart:io';
import 'package:http/http.dart' as http;

void main() async {
  ...
}

And then:

chmod +x bin/server.dart

With that, the bin/server.dart file becomes executable itself. So you can run it as:

bin/server.dart

And if you read my post on Hot reload for Dart, you can apply that here too:

nodemon -x bin/server.dart -e dart
Magic pixel View count: