Can I use npx with pnpm? - npm

Can I use npx together with a pnpm install?
It doesn't seem to work and pnpx fetches remote dependencies, it seems more like npm create.
Is there a way to use npx with pnpm to execute local binaries or is there a different pnpm equivalent?

The are are two keys uses of npx.
Running executables inside your downloaded dependencies
For example npx jest.
The pnpm equivalent is pnpm exec jest.
Running executable commands in packages you want to download transiently
For example npx create-react-app my-app.
The pnpm equivalent of this is pnpm dlx create-react-app my-app.
Note: There used to be a pnpx command, but it has been deprecated (details here).

Related

What is difference between pnpm create, pnpx, dlx?

The pnpm package manager offers three commands that are alternatives for npm's npx command. These are pnpm create, pnpx and pnpm dlx. All three seem to do the same thing. But what are the differences between them? Which one should be preferred for which tasks? Or is there a universal best?
As of v7, pnpm dlx is the same as pnpx. It downloads a package and executes it.
pnpm create is a shorthand for pnpm dlx, when you need to create an app. So, for instance, pnpm create react-app my-app will download the create-react-app package and run it to bootstrap a react app. It is the same as running pnpm dlx create-react-app my-app.
There is also pnpm exec, which doesn't download a package just runs a package that is already in node_modules/.bin

Understanding difference between npx and npm

I'm struggling to develop a deeper understanding of npx. So in particular the difference between running a commmand with npm and npx. I understand that npx can execute a package from a URL, just one local npm package etc.. But for example here:
npx lerna run start --scope frontend --stream
What is the difference between
npx lerna run start
and
npm lerna run start
?
NPM - Manages packages but doesn't make life easy executing any.
NPX - A tool for executing Node packages.
NPX comes bundled with NPM version 5.2+.
NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.
When executables are installed via NPM packages, NPM links to them:
1.local installs have "links" created at ./node_modules/.bin/ directory.
2.global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

Is there any Yarn equivalent of npx preact create?

I am trying to run the preact create command using Yarn. It's simple to run preact create ..... ..... or npx preact create ... ..... It works fine and both of the commands use npm. But I am trying to run the command using Yarn. I have tried the following commands but nothing works. The error says "couldn't find a package.json file in path".
yarn preact create .... ....
yarn dlx preact create .... ....
yarn preact-cli create .... ....
yarn dlx preact-cli create .... ....
So, what should I do to run the command using Yarn. One alternative maybe is running the command using npm and then running yarn install and then running npm uninstall. But what's the actual way?
There is no Yarn equivalent for npx. The Yarn team thought npx was enough.
That being said, we support creating a new Preact CLI project with Yarn through the --yarn flag.
npx preact-cli create ... --yarn
https://github.com/preactjs/preact-cli#cli-options
Please do note that when using Preact CLI via NPX, you do need to use preact-cli. npx preact gives you the Preact library.
In general, as of 2021, there is no equivalent for npx
Feature was requested in yarn, but went not really solved
https://github.com/yarnpkg/yarn/issues/3937
So package developers need to think for both npm and yarn users.
You can use ynpx as an equivalent.
yarn global add ynpx
Then do your npx commands as normal, using ynpx in place of npx
eg.
ynpx preact create ...
For a TL;DR answer from the thread mentioned above.
You can find the following.
npx performs no operations which clash with people using other package managers [...] So you could say npx is ypx, unless you feel really strongly about cache-sharing, which is a pretty thing.
For the probable reasoning of not creating a ypx specific command
Yarn now supports create cli as following
yarn create [your-cli-name] [..arugments]
for eg. yarn create expo-app hello-world

Why sould one init a react-native project with npx?

Today I tried to do things differently, and launched a react-native project with npx, and went into troubleshooting issues like react-native command not found, are you sure this is an android project, etc. while things were easier with expo and react-native init.
So I'm wondering, according to you, what are the advantages, what does npx more ?
npm:
npm create-react-app my-app executes the local create-react-app package from your machine, so you first have to install it globally on your system with npm install -g create-react-app.
npx:
If you run npx create-react-app my-app and don’t have create-react-app globally on your system, it will get downloaded and not installed globally.
As you are getting "react-native command not found", first make sure to have react-native cli installed globally. For that,
npm i -g react-native-cli
Then create your react-native project.

NPM doesn't support PnP

I was trying use pnpm with create-react-app v2 as follows
npx create-react-app my-app --use-pnp
But it gives me following warning
NPM doesn't support PnP.
Falling back to the regular installs.
What should I do in order to use pnpm with create-react-app?
I already have pnpm installed globally using npm.
pnp is a Yarn feature, you need the latest version of yarn installed on your system to use it (at least Yarn 1.12).
Try
yarn create react-app project-name --use-pnp
And make sure yarn -v is >= 1.12