Can you use Yarn2 PnP "zero-installs" on a machine without Yarn?

1.1k views Asked by At

I'm playing with Yarn 2's "zero install" stuff for a minor tool to address one of my boss's random allergies. (He takes umbrage at the expectation of having to run npm i on a cloned repo to make it work and insists putting node_modules into version control is somehow not a godawful idea; so I want to use this as an excuse to sneak in Yarn and also stop him from powering that through.)

As I understand, what "zero install" basically means is Yarn tries to make putting dependency installation state into VCS actually feasible. However, to run the actual app, Yarn needs to replace Node's dependency resolution with its PnP mechanism. This happens automagically for Node instances run from Yarn scripts, but running Yarn scripts requires Yarn to be available. (And remember, we're trying to solve the problem of somebody being arbitrarily stubborn about installing things.)

The best I have is making my start script be npx yarn node app.js, but that feels unnecessarily convoluted; after all, with Yarn 2, the tool itself is stored in .yarn/releases and the global yarn command uses that, but that's a huge minified blob of some bundler's output, I don't know how I'd begin invoking that.

1

There are 1 answers

1
Viktor Vlasenko On BEST ANSWER

To register PnP runtime produced by Yarn it is enough to just require .pnp.js from command line, so you can run your app.js via:

node -r ./.pnp app.js

There is another way to do the same: you can require .pnp.js from within app, but when you do it not from command line, you must also call setup function on returned PnP API instance, just add this line on top of app.js:

require('./.pnp').setup();