How do I install a Dart package globally?

Issue

Does Pub Package Manager provide a way to install packages globally?

I have been a node.js developer for a while and I was wondering if there was a pub equivalent of npm install -g <package_name>

If there is a way to install packages globally, is there a way to register binary scripts which can be installed to be invoked like shell commands.

Solution

dependencies

All pub packages are installed globally (a unique subdirectory below ~/.pub-cache/ for Mac/Linux AppData\Roaming\Pub\Cache in Windows, for each package version) and is then only linked to your project you add the dependency in pubspec.yaml.

Actually ~/pub-cache is only global for a user not global as for the entire system. I don’t know npm well but I think npm install -g installs it global to the system. There is nothing similar in Dart but you don’t install a package in Dart anyway. When you use a Pub package in the same version in different packages it is saved only once on the drive.

global package executables

Packages containing executable scripts can be globally (per user) activated using pub global activate .... If you add the ~/.pub-cache/bin/ directory to your path you can run these executable Dart scripts just by their name (like tuneup for the tuneup package) or with pub global run packagename:scriptName (like pub global run tuneup:tuneup) If script and package name are the same you can omit the :scriptName part.

local package executables

You can also run scripts from dependencies of your current project with pub run packageName:scriptName (like pub run test for running unit tests)

Answered By – Günter Zöchbauer

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.