nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.2k stars 1.75k forks source link

Use `npx` instead of installing the CLI globally #2756

Open micalevisk opened 1 year ago

micalevisk commented 1 year ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

since we will start supporting only node16 or greater in nestjs v10, I think it is a good time to start encourage devs to use npx as a good alternative to installing @nestjs/cli globally.

NPX is installed automatically with NPM version 5.2+ And looks like node16 comes with NPM v8 when we use NVM:

image

Then I'm confident that most people that are using node16 would also have NPX installed.

As of now, we got a hint on this already:

image

This is good because from time to time we got questions at our Discord server due to some mismatch between nest vs npx nest. Using the local @nestjs/cli is always better to me.

But I found that using npx @nestjs/cli@latest new could be tedious or too magic to new users or to folks that aren't used with npx.

Describe the solution you'd like

Others popular frameworks are using npm-init command (which has create as alias), like NextJS (npx create-next-app), SST (npm create sst) and Svelte (npm create svelte@latest myapp)

I'm proposing to change the https://docs.nestjs.com/cli/overview#installation page to replace npm install -g @nestjs/cli with npx create-nestjs (or npm create nestjs as we can use yarn create and pnpm create as well). And start using npx nest instead of nest on code snippets that are showing nest's CLI commands.

Teachability, documentation, adoption, migration strategy

We will need to touch the docs a bit.

Extra: an improvement to avoid installing the CLI globally I've made the [`create-nestjs`](https://www.npmjs.com/package/create-nestjs) package with that in mind already and the code is tiny: https://github.com/micalevisk/create-nestjs/blob/main/run-nestjs-new.js It just call `npx @nestjs/cli@latest new [...args]` under the hood. I could move that repository and change the onwership of that package to nestjs org if needed. Thus we can already use those commands, see (linux): [demo-npm-create.webm](https://user-images.githubusercontent.com/13461315/236647705-1c21d53a-2755-4cc4-9456-974c47a09b15.webm) [demo-yarn-create.webm](https://user-images.githubusercontent.com/13461315/236647709-40fa360d-5872-44b8-b53b-1ad2086981a3.webm) [dem-pnpm-create.webm](https://user-images.githubusercontent.com/13461315/236647711-88e75b34-1d80-4c19-b34b-5363882a75a4.webm)

What is the motivation / use case for changing the behavior?

micalevisk commented 1 year ago

Ideally, nest (global) should work with any version of nestjs installed locally but I don't think this is feasible.