Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yarn equivalent of npx ? #3937

Closed
sandys opened this issue Jul 15, 2017 · 41 comments
Closed

yarn equivalent of npx ? #3937

sandys opened this issue Jul 15, 2017 · 41 comments

Comments

@sandys
Copy link

sandys commented Jul 15, 2017

how is yarn recommending we use npx (that is now core part of npm - npm/npm#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 ?

@Daniel15
Copy link
Member

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.

@arcanis
Copy link
Member

arcanis commented Jul 17, 2017

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.

@zkat
Copy link
Contributor

zkat commented Jul 17, 2017

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: npm/npm#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)

@BYK
Copy link
Member

BYK commented Jul 17, 2017

@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?

@MarkBennett
Copy link

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.

@pygy
Copy link

pygy commented Nov 8, 2017

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.

@SimenB
Copy link
Contributor

SimenB commented Dec 14, 2017

@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

@tdmalone
Copy link

@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?

@dcorking
Copy link

dcorking commented Jan 8, 2018

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

@dcorking
Copy link

dcorking commented Jan 8, 2018

@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.

@deepsweet
Copy link

deepsweet commented Apr 24, 2018

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.

@freddi301
Copy link

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

@phra
Copy link

phra commented May 23, 2018

@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

@amir3code
Copy link

@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

@alireza-mh
Copy link

@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.

@factoidforrest
Copy link

Can we get a yarnx ?

@arcanis
Copy link
Member

arcanis commented Nov 3, 2018

@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 🙂

@zkat
Copy link
Contributor

zkat commented Nov 3, 2018

@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 :)

@paul-pro
Copy link

paul-pro commented Dec 17, 2018

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.

@factoidforrest
Copy link

factoidforrest commented Dec 22, 2018 via email

@arcanis
Copy link
Member

arcanis commented Dec 22, 2018

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

@sospedra
Copy link

sospedra commented Jun 5, 2019

Does the new yarn dlx solve this issue?

cc @sandys @arcanis

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

@arcanis
Copy link
Member

arcanis commented Jun 5, 2019

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?).

@silverwind
Copy link

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.

@BYK
Copy link
Member

BYK commented Jul 3, 2019

@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?

@rulatir
Copy link

rulatir commented Jan 20, 2020

@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.

@rulatir
Copy link

rulatir commented Jan 20, 2020

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.

bluelovers added a commit to bluelovers/ws-ypx that referenced this issue Jan 28, 2020
@bluelovers
Copy link

try this
https://www.npmjs.com/package/ynpx

@xgqfrms
Copy link

xgqfrms commented Mar 22, 2020

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

@codekiln
Copy link

codekiln commented Aug 5, 2020

@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".

@pdfrod
Copy link

pdfrod commented Sep 23, 2020

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.

@kuworking
Copy link

@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

@delanym
Copy link

delanym commented Nov 5, 2020

@bluelovers
Copy link

https://yarnpkg.com/en/docs/cli/exec

404: Page Not Found

@karlhorky
Copy link

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

@htfy96
Copy link

htfy96 commented Dec 25, 2020

For anyone reading this trying to find a quick alternative to npx -p @storybook/cli sb init, you can use:

yarn global add @storybook/cli
yarn exec sb init

@somethingelseentirely
Copy link

So once you want to use babel, you have to also use npm, to get npx?

@megawebmaster
Copy link

If anyone else is wondering: you can use npx to run immediately with yarn too, i.e. npx sb init calls yarn install after packages are added to package.json 👍

@merceyz
Copy link
Member

merceyz commented Jan 6, 2021

If you use npx to run a local binary then you can just use yarn <bin name>. In Yarn v2 there is the yarn dlx` command to download and execute a package once

@bluelovers
Copy link

nobody care about this?
https://www.npmjs.com/package/ynpx

@koszonetdoktor
Copy link

@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".

One command:
npx ynpx sb init

@yarnpkg yarnpkg locked as resolved and limited conversation to collaborators Feb 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests