yarn equivalent of npx ?

Created on 15 Jul 2017  ·  35Comments  ·  Source: yarnpkg/yarn

how is yarn recommending we use npx (that is now core part of npm - https://github.com/npm/npm/pull/17685).

Basically, it acts somewhat like ruby's "bundle exec", other than that it launches the package manager if dependencies are missing. This will sidestep yarn and switch back to npm.

Is there a plan to create a "ypx" equivalent that leverages yarn to do this ?

cat-feature

Most helpful comment

@sandys - The issue @zkat referenced (npm/npm#6053) is not an issue on yarn since you can just do yarn x (or yarn run x if you want to be explicit) if x is in your ./node_modules/.bin directory. So I don't think there's a pressing need for npx equivalency.

If you think there's a strong need, can you explain why you need it? Eg. what problem will it solve for you?

All 35 comments

For what it's worth, I think npx was inspired by "yarn create" which is similar but only works for packages prefixed with "create-". Not sure what the plans are here.

Yup, we currently have yarn create (try using yarn create react-app, for example). We might open it to others verbs in the future, but that's not on the roadmap yet.

I'm working on library-ifying npx. It's not a huge task to grab the existing npx code and just replace the npm-related guts with the yarn-equivalent commands.

I won't add that directly to npx itself, since it's _meant_ to be agnostic: npx performs no operations which clash with people using other package managers. It doesn't even require npm to be on the system, so you can npm rm -g npm and npx will work just fine. So you could say npx _is_ ypx, unless you feel really strongly about cache-sharing, which is a pretty thing.

(in re inspiration: npx is primarily inspired by this long-standing feature request: https://github.com/npm/npm/issues/6053. Most of its functionality centers around fulfilling _this_ need. The auto-install feature _was_ added post-yarn-create, and is definitely intended to be an actual generalized solution to that particular thing -- but it does _way more_ than that)

@sandys - The issue @zkat referenced (npm/npm#6053) is not an issue on yarn since you can just do yarn x (or yarn run x if you want to be explicit) if x is in your ./node_modules/.bin directory. So I don't think there's a pressing need for npx equivalency.

If you think there's a strong need, can you explain why you need it? Eg. what problem will it solve for you?

Does anyone know if yarn exec is similar to bundle exec? I see it on the CLI but not in the docs on the website. A quick play with yarn exec on the command line seems like it runs installed binaries so it might solve your problem @sandys.

FWIW, npm-run is an older utility that allows one to run local node_modules binaries, and it doesn't depend on npm. It doesn't have any options though, whereas npx is full of knobs.

@BYK Use case is running e.g. ypx greenkeeper-lockfile@1 or ypx danger@2 on CI without adding those as deps to the project itself

@SimenB CI usually wouldn't commit the project back to version control though, so it doesn't matter if it adds the dependencies in the process, right?

@MarkBennett Since yarn exec doesn't run scripts from package.json I don't think it is the right place to add this feature.

@BYK I am not the OP, but I came to this feature request because I had a package on my local machine but not in my package.json. Therefore my app would run for me, but not for anyone who did a clean install of my app. This is a feature of ruby bundler's bundle exec that I like - it won't run unless all the deps are in the manifest.

My main complain about yarn x is that it's trying to resolve a target from 3 (three) different places: yarn internal commands, npm scripts and bins.

Let's say I have a tool with binary named check: 1) yarn check will run its own internal check command instead 2) yarn run check will run a consumer's npm script with such a name or maybe my tool.

npx gives a strong separation of concepts: yarn x is always an internal command, yarn run x is always a script, and npx x is always a binary, no need to guess and hope.

something like gist

#!/usr/bin/env bash

package_name=$1
temp_dir="/tmp/ypx/$package_name/$(date +%s%N)"
mkdir -p $temp_dir
(cd $temp_dir; yarn add $package_name) && (PATH="$temp_dir/node_modules/.bin":$PATH; "$@")
rm -rf $temp_dir

@BYK another use case is running a _binary_ of a package that is not installed locally yet and i want to run it one time, without bothering to delete it on my own after. npx function is an extension of the behavior of yarn x because you can:

  1. run a command of a local package in the ./node_modules/.bin/ in the current directory
  2. if the package is not present locally OR the ./node_modules/ directory doesn't exist, download the package in a temporary directory with its dependencies and invoke the command

this is done transparently to the user.

a hypothetic ypx can also offer a third point that is:

  1. if the package is not present locally AND it's present in the yarn cache AND it matches the latest version, invoke the command using the cache instead of download all the packages

@BYK It does not work.
Install babel-cli for example: yarn add babel-cli
Then run yarn babel-node --presets es2015 ./server.js which server.js is a file in the current directory and is a simple express api server.
It simply does not work and says that the file does not exists. (Error: Cannot find module)
But using it with npx works npx babel-node --presets es2015 ./server.js

@BYK as far as i know npx will look for your command on node_module/.bin/ on local machine and if it didn't find proper command it will get package from web if there is any and you can be always up to date.
yarn do not get package from web while it's not installed on local machine.

Can we get a yarnx ?

@light24bulbs why in particular?

Frankly I think npx is a good tool even if a bit too "npm inc."-centric (as many of npm's tools, to be fair). A yarnx wouldn't solve this problem (it would necessarily be yarn-centric), so I'm not too sure it would be a good idea.

Ideally, I would prefer npx to automatically detect the package manager to use, or at least to allow configuring it inside an rc file. I'd suggest to bring this issue to them and see what they say. Depending on their answer we'll then be able to have an informed discussion 🙂

@arcanis npx itself is comboed with npm because it's bundled with npm -- libnpx is not, and in fact, that's what pnpx from pnpm uses under the hood. I added a couple of patches to make it possible for Zoltan. I won't be adding auto-detection support because it removes some of the integration and makes things more complicated and harder to support :)

Just googled this issue and I think it's appropriate place to ask about any updates. Is there any existent tool/solution, or plan to add some functionality to yarn?
For example my current issues with npx are:

  1. it downloads absent package with dependencies every time,
  2. it creates package-lock.json in Yarn oriented projects which causes warnings and need to remove it by hands.
    (specifically I've just execute npx gatsby new blog https://github.com/gatsbyjs/gatsby-starter-blog)

Both of this issues looks like perfect match to solve for yarn like @phra already mention as his third point.

UPD: So basically main reason for ypx I mention is not binary execution (which is totally fine with yarn), but the ability to autodownload packages I intend to execute.

I agree. I also think there is an opportunity to improve on NPMs weird API
a bit. I think calling yarn exec COMMAND would make more sense than
yarnx. Ruby bundler had a very similar command
https://bundler.io/man/bundle-exec.1.html

On Mon, Dec 17, 2018 at 2:29 PM Pavel Prokudin notifications@github.com
wrote:

Just googled this issue and I think it's appropriate place to ask about
any updates. Is there any existent tool/solution, or plan to add some
functionality to yarn?
For example my current issues with npx are:

  1. it downloads absent package with dependencies every time,
  2. it creates package-lock.json in Yarn oriented projects which causes
    warnings and need to remove it by hands.
    (specifically I just execute npx gatsby new blog
    https://github.com/gatsbyjs/gatsby-starter-blog)

Both of this issues looks like perfect match to solve for yarn like @phra
https://github.com/phra already mention as his third point.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/yarnpkg/yarn/issues/3937#issuecomment-447968558, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAjhEng6juvpaK4xLx1KrVHccJAytnAfks5u5_CcgaJpZM4OY9aL
.

--
-Forrest

yarn exec already exists (with a different meaning than what you suggest) 🙂

Does the new yarn dlx solve this issue?

cc @sandys @arcanis

https://yarnpkg.github.io/berry/cli/dlx

Yep! Closing this issue for now, I haven't figured out yet whether we'll want to backport the feature to the v1 (probably not?).

I'm not sure it's a worthy replacement. yarn dlx eslint --help takes 2.7 seconds on my machine while npx eslint --help finishes in 0.2s. If one calls a lot of bin scripts, this will quickly add up to unacceptable values.

Also, I think stdout/stderr should not be written to by yarn unless there is an error, to allow parsing the script's output.

@silverwind the timing difference seems quite stark. Is this reproducible in multiple runs consistently? If yes, I'd file a new issue for investigation as we surely don't want it to be slow.

Regarding stdout/stderr from Yarn, I _think_ you can use yarn --silend dlx eslint to suppress all non-critical Yarn output. @arcanis can you confirm this last one?

@light24bulbs why in particular?

For all people who find some package on the web, see that installation instructions involve npx something something, but want to remain in the yarn world.

Does the new yarn dlx solve this issue?

Is yarn dlx an exact s/npx/yarn dlx/ drop-in replacement for npx? If not, then it doesn't solve this issue.

yarn create & npx & npm init

demo

https://www.npmjs.com/package/create-react-app

$ yarn create react-app

$ npx create-react-app

$ npm init react-app

image

https://www.npmtrends.com/npm-vs-npx-vs-yarn

image

@light24bulbs - to echo @rulatir, I got here because Storybook's quick start guide says to use npx to install it, and I didn't have an easy recipe for how to incant the equivalent spell with yarn. If there is an equivalent set of commands for yarn, we should post them on the yarn website, so that page (instead of this thread) is at the top of google search results for "yarn version of npx".

I'm also on the same boat as @codekiln. Whenever I'm following some instructions that say run npx ..., I have no idea what's the yarn equivalent. An example is npx tslint-to-eslint-config.

@light24bulbs - to echo @rulatir, I got here because Storybook's quick start guide says to use npx to install it, and I didn't have an easy recipe for how to incant the equivalent spell with yarn. If there is an equivalent set of commands for yarn, we should post them on the yarn website, so that page (instead of this thread) is at the top of google search results for "yarn version of npx".

Same here, with capacitorjs installation guide https://capacitorjs.com/docs/getting-started, the feeling is that this persuades the user to abandon yarn and go back to npm

I guess @delanym meant this page (but I don't think this replicates npx): https://yarnpkg.com/cli/exec

Was this page helpful?
0 / 5 - 0 ratings