Activating a dart app with pub global activate

Issue

I created a new Dart app like this:

dart create hello

I can run the app like this:

dart run hello/bin/hello.dart

I try to activate the app like this:

dart pub global activate --source path hello

But I can’t run the file like I would expect:

hello

zsh: command not found: hello

The .pub-cache/bin folder is in my cache but pub global activate didn’t put it there.

This does work:

dart pub global run hello

Hello world!

But I’d like to be able to run the script without typing dart pub global run every time.

If I do a package from pub.dev it works ok:

dart pub global activate webdev

It puts a webdev executable inside the .pub-cache/bin folder and I can run it.

webdev --version

2.7.4

So is there another step I need to do to make my hello app get into the executable folder?

I also tried compiling it:

dart compile exe hello/bin/hello.dart

And activating it again:

dart pub global activate --source path hello

But there is still no binary in the .pub-cache/bin folder. Any suggestions?

Update

After getting Kevin’s answer below, I added the following to pubspec.yaml:

executables:
  hello:

Then I ran the following command:

dart pub global activate --source path hello

which gave the following result (username modified):

Resolving dependencies... 
Got dependencies!
Package hello is currently active at path "/Users/suragch/Dev/DartProjects/hello".
Installed executable hello.
Activated hello 1.0.0 at path "/Users/suragch/Dev/DartProjects/hello".

But if I run this:

hello

I get the following error:

/Users/suragch/.pub-cache/bin/hello: line 7: pub: command not found

Running this still works, though:

dart pub global run hello

Hello world!

Solution

So it looks like this problem was caused because because of a bug as a result of switching from pub to dart pub. If you open the .pub-cache/bin/hello file with an editor, you’ll see the following:

#!/usr/bin/env sh
# This file was created by pub v2.13.1.
# Package: hello
# Version: 1.0.0
# Executable: hello
# Script: hello
pub global run hello:hello "$@"

The error about line 7 is the last line where you can see it is referencing plain pub. Change that line to the following:

dart pub global run hello:hello "$@"

Now you can run your app from anywhere:

hello

Hello world!

This is only a temporary workaround. Follow this issue for updates. Thank you to Kelvin Omereshone for pointing me in the right direction.

Answered By – Suragch

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *