21

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?

6 Answers6

23

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.

rschristian
  • 1,730
  • 1
  • 6
  • 15
  • 3
    Thank you @rschristian for this useful answer. But the above line of code doesn't work. It still uses npm to install the dependencies. But thank you for informing me about the --yarn flag. I used it while running `preact create ...... ...... --yarn` and it works fine. Though `preact create` isn't an alternative to npx. –  Mar 13 '21 at 14:27
  • 5
    You're saying `npx preact-cli create ... --yarn` doesn't install using yarn? Mind raising an issue in our tracker? I can't reproduce and I don't see how that could happen but it's clearly something we need to address. – rschristian Mar 13 '21 at 23:13
  • 3
    Yeah, it's not working. Well, I'll open an issue in the github repo of preact. –  Mar 14 '21 at 16:19
  • 3
    Make sure it's Preact-CLI, not Preact. Different things. But I'll take a look there. – rschristian Mar 14 '21 at 22:12
  • Hmm.. I tried running the command again before raising a issue in issue tracker. But now I see that it uses npm at first to install preact-cli (as I didn't install preact-cli using yarn). Then it uses to yarn to create the preact-app. So, it's normal I think and I am not raising an issue.. –  Mar 15 '21 at 15:51
  • 4
    Ah, yeah, that's normal. NPX, if you didn't know, is a way or globally installing an app with NPM that is not accessible, i.e., you can't actually use it outside of NPX. The install will then be deleted overtime when the cache is cleared. NPX is used to setup Preact-CLI, as Yarn does not offer an equivalent, but we use the package manager of your choice for installing the dependencies into your new project. – rschristian Mar 15 '21 at 17:13
7

UPDATED: there is yarn dlx in v2

Run a package in a temporary environment.

https://yarnpkg.com/cli/dlx

Feature was requested in yarn https://github.com/yarnpkg/yarn/issues/3937

So package developers need to think for both npm and yarn users.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
4

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 ...
robrecord
  • 504
  • 5
  • 15
3

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

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
-1

Yarn now supports create cli as following

yarn create [your-cli-name] [..arugments]

for eg. yarn create expo-app hello-world

Dipak Parmar
  • 11
  • 1
  • 2
  • This is not relevant for `preact-cli`. `yarn create` and `npm init` only work with packages prefixed with `create-`, i.e,, `create-foo` -> `yarn create foo`. Preact-CLI, at this time, does not offer a package in this format so your answer is wrong in context. – rschristian Aug 26 '22 at 19:41
  • correct, my answer is more general and not relevant to `preact-cli`. – Dipak Parmar Aug 27 '22 at 21:37
  • 1
    If it's not relevant for `preact-cli` then it's not relevant to this question. This question is about how to initialize a `preact-cli` project with yarn. – rschristian Aug 29 '22 at 04:07
-1

One alternavite would be using vite with yarn.

yarn create vite your-project --template=preact
  • 1
    This has nothing to do with the question. The user was asking how to create a new Preact-CLI project with Yarn; Vite is a separate tool entirely. – rschristian Apr 30 '23 at 07:34
  • 2
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/34308554) – Rohit Gupta May 01 '23 at 03:48
  • @rohit-gupta, rschristian True, this answer doesn't address the preact-cli issue, Only gives alternative way to create preact app using yarn and vite – Thusara Senanayake May 01 '23 at 06:21
  • 1
    That's not how this site works. Do not provide answers that you know do not address the issue the OP is facing. It provides no value. – rschristian May 01 '23 at 20:11