DEV Community

Discussion on: Introduction to NextJS

Collapse
 
maxdevjs profile image
maxdevjs

Thank you, this is an enjoyable intro. Also, the links are actually useful to better grasp the concepts.

Does npx actually install create-next-app or it is a one-off run? I guess it is the second option, but I can be wrong about it.

Collapse
 
zenulabidin profile image
Ali Sherief

I admit it took me a while to find the answer to that question, but my reasoning for this is that since npx is meant to directly run packages that are not global, so local packages, it does indeed install create-next-app, just in the project's node_modules/ folder, not the global one.

Scott Logic has a good article about how npx works, you should read that too.

Collapse
 
maxdevjs profile image
maxdevjs • Edited

Thank you for the reply. I believe you, I am also digging into it a bit and things are still not very clear.

In fact, what I (thought that I) knew is that npx can also run in a temporary way packages without the need to install them (globally/permanently).

Point is, when using npx I do not find local installation of packages (neither in node_modules/ folder, cache, etc) that hadn't been previously installed.

In my understanding, npx acts in different ways.

First, as in the Scott Logic's article that you provide, npx can execute local packages when they are already installed:

Make sure you --save or --save-dev the package first. This keeps dependent packages listed in package.json, so that npx can use the local version instead of downloading it to the npm cache.

I tracked down my first idea to Introducing npx: an npm package runner:

npx can also execute one-off commands as in the case of npx create-react-app my-cool-new-app, which installs a temporary create-react-app and calls it, without polluting global installs.

chaining the previous statement with the following

By the time you run them again, they’ll already be far out of date, so you end up having to run an install every time you want to use them anyway.

and with the npx(1) documentation

If a full specifier is included, or if --package is used, npx will always use a freshly-installed, temporary version of the package. This can also be forced with the --ignore-existing flag.

makes me think that there are not really clear and explicit explanations about how npx works in this determinate case (create-next-app but also other one-off commands) but, yes, if a package is not already installed (saved in package.json) it temporarily install the package, runs it, and then delete it (because for each creation of a specific project, the package will be used only once):

in my system it temporarily install the package in $HOME/.npm/_npx, runs and then delete it. Solved :)

npx can do a lot more, as to run commands with different Node.js versions, but that's another question.

Obviously, this is a personal curiosity :)