What is difference between pnpm create, pnpx, dlx? - npm

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

Related

Can I use npx with pnpm?

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

how do i add yarn package to react native project installed with npm?

Note: Do guide me if something is missing.
So, I wanted to install a package from https://rnfirebase.io/auth/usage, but I have an npm project. The command on the website has only for yarn. I don't want to add yarn to project because (Is there any harm in using NPM and Yarn in the same project?) it states that it is not recommended.
So, then how do I install it with npm?
You have to use yarn, or you can look for a package that has the functions that you are looking for using npm
You can install it with npm just fine, don't worry. They are all package managers installing npm packages from the same repository. There is no difference in what you are installing or how they are installed. You can get different node_module structures, but for yarn you need config for that.
Yes its not recommended because it generates different lockfiles that will dictate different structures and versions in your node_modules folder. You want multiples devs to have the same "experience". However, lots of JS frameworks will come pre-configured with yarn, like React Native and you just end up having two lockfiles. One for npm and one for yarn. There is no harm in deleting the yarn file and keeping the package-lock. If you delete both, a new lockfile for the package manager you are using will be generated on npm i | yarn i | pnpm anyway.
To install it with npm just use npm i <PACKAGE_NAME> so npm i #react-native-firebase/app.
Here is the npm repo page for that package, https://www.npmjs.com/package/#react-native-firebase/app, notice the install command is npm! Only reason firebase devs only mention yarn is because they are hipsters ;)

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

Globally installation of packages with npm

I have a question regarding the package manager npm and the meaning of installing the package globally.
For example I work often with react, should I install react globally?
npm install -g react react-dom
Does this mean that next time when I do
npx creat-react-app my-app
It will get the package from the global or it does not matter and it will still download it locally inside my-app?
Because I really do not understand the idea behind installing globally.
Because if I want to use a package it should be mentioned in package.json, if it is in package.json it is then located in node_module ... so yeah ...
Could anyone give me better insight?
Thanks in advance
React library can be installed globally on your local machine. In development there is no real reason to do that since you might not have the latest version and this might cause issues.
Better practise is to use react on project level by using the command you stated above npx create-react-app my-app
If you push code to a server the package.json file will install the dependencies (React, React DOM,..) to build your project.
Read more here : https://create-react-app.dev/docs/getting-started/
If you've previously installed create-react-app globally via npm
install -g create-react-app, we recommend you uninstall the package
using npm uninstall -g create-react-app or yarn global remove
create-react-app to ensure that npx always uses the latest version.
You can check what packages are installed globally using:
npm list -g --depth 0

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