12

I might be having a mental eclipse, but it's hard for me to do the simplest thing: write a simple library and use it. (I want to do this all locally, without publishing the library, etc.)

I've browsed through the list of pub commands and none seems to install a package locally, so how can I use it in other projects?

For example, I just cloned this library and would like to test it locally to see if it works. How can I do that?

hopper
  • 13,060
  • 7
  • 49
  • 53
Binders Lachel
  • 505
  • 1
  • 5
  • 14
  • 1
    It seems you confuse package and library. In Dart a package consists of one or more libraries, also a package with only resource files without any library is possible. A library is a Dart script file. There could also be additional files be part of that library (connected with `part`/`part of`. If you want to use a library within the same package, just import it `import 'some_library_file.dart';` – Günter Zöchbauer Jun 04 '15 at 21:17

3 Answers3

17

To use a local library, you can specify a local filepath in your pubspec, such as:

dependencies:
  transmogrify:
    path: /Users/me/transmogrify

When using a local dependency, Dart will pick up any changes automatically, so there's no need to run a special pub command.

hopper
  • 13,060
  • 7
  • 49
  • 53
  • 4
    Why does it sound absurd? It's a single-line change to do exactly what you want. Honestly, in my opinion requiring a separate pub command to link a local package would be more absurd :) – hopper Jun 04 '15 at 19:39
  • yet all other package managers I know work this way. They have a repo and you fill it, either from the web as dependencies, or from any other source. Maven, npm, dub, ... – Binders Lachel Jun 04 '15 at 19:42
  • Yeah, it's definitely common - I'm just not convinced it's any better. :) For what it's worth, npm supports using [local filepaths](https://docs.npmjs.com/files/package.json#local-paths) this same way. And I don't think I would point to Maven as an example of a modern package manager done right... ;-) – hopper Jun 04 '15 at 19:49
  • 1
    What I don't like with this approach is that I don't want to change the `pubspec` file to use my local version of a _git repo_ type dependency. e.g.: In a JS package, [`npm link`](https://docs.npmjs.com/cli/link) let me use packages locally without modifying the `package.json` file. – Emile Bergeron Oct 08 '17 at 23:43
2

You have to use path packages.

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • 1
    Why do you think that? I strongly recommend you to read all the article linked to better understand the dependency system used in pub and all the possibilities it offers. – Alexandre Ardhuin Jun 04 '15 at 19:42
  • I just want to create a lib and use it. This way forces me to remember where I store it, because I can't publish it / install it. Anyways, it's more like a makefile than a dependency manager I guess – Binders Lachel Jun 04 '15 at 19:44
  • 1
    Keep in in the same place as your pubspec if you want. the "path:" config accepts relative paths. – computmaxer Jun 05 '15 at 17:35
0

You can specify also relatively with the name of the package, e.g.:

dependencies:
   my_other_package:
      path: ../my_other_package
Oleg Novosad
  • 2,261
  • 1
  • 27
  • 28