How to create and run a Dart project

Set up a new Dart project, add dependencies, and run it

Create a new project

Use the dart create command to scaffold a new Dart project:

dart create myapp
cd myapp

This generates a project with:

  • pubspec.yaml — dependencies and metadata
  • bin/main.dart — entry point
  • lib/ — library code
  • test/ — test files

Add dependencies

Edit pubspec.yaml or use the CLI:

# Add a package
dart pub add http

# Add a dev dependency
dart pub add --dev test

Then fetch:

dart pub get

Run

# Run the main entry point
dart run

# Or run a specific file
dart run bin/main.dart

Compile

# Compile to native executable
dart compile exe bin/main.dart

# Compile to JavaScript
dart compile js -o main.js lib/main.dart

Run tests

dart test