Yarn: Using yarn as a library

Created on 21 Sep 2017  路  5Comments  路  Source: yarnpkg/yarn

Background
Prior to yarn 1.0, the packaged version of yarn made it possible to access yarn's internals programmatically. This is useful for projects like Dependabot, which effectively use yarn as a library.

Specifically,

  • We use yarn for loading the manifest and lockfile. This could mostly be reimplemented by manually loading the manifest, and using the new @yarnpkg/lockfile package, but we also use a few other bits of yarn's internals (e.g. filtering out packages that use exotic resolvers).
  • We inherit from the Add command to perform a "lightweight" update - that is, an update that generates a new lockfile without actually downloading the packages.

Since yarn is now distributed as a single file (cli.js), accessing internals is no longer possible.

Shelling out to yarn's CLI would get us some of the way there. But for tools that integrate deeply with yarn (such as Dependabot), having access to the internals means we can build a far more robust integration.

There are a couple of options that could enable this:

  1. Moving back to distributing multiple sources files in the main package
  2. Splitting out the bulk of yarn into a "yarn-core" package, which is designed to be used as a library, and keeping "yarn" purely as a thin CLI wrapper around "yarn-core".

I'm also aware of #3397, which seems to be what triggered the creation of @yarnpkg/lockfile.

I'd love to hear your thoughts!

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
You can no longer require yarn internals (e.g. const { Install } = require("yarn/lib/cli/commands/install");), so yarn can't be used as a library.

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
const { Install } = require("yarn/lib/cli/commands/install"); works.

Please mention your node.js, yarn and operating system version.
Node 8.2, macOS Sierra.

Most helpful comment

I'm also wondering if I can access yarn programmatically. For example:

const sh = require('shelljs');
const yarn = require('yarn');
yarn.install(sh.pwd());

All 5 comments

I'm also wondering if I can access yarn programmatically. For example:

const sh = require('shelljs');
const yarn = require('yarn');
yarn.install(sh.pwd());

would love to use it programmatically from within electron 馃憤 馃榿

Any progress on this?

Yarn 2 core will be usable as API
https://github.com/yarnpkg/berry/blob/master/README.md#generic-packages

Was this page helpful?
0 / 5 - 0 ratings