Why does npx install webpack every time?
Asked Answered
M

2

25

I have a JavaScript app I'm bundling with webpack. Per the docs, I'm using this command to start bundling:

npx webpack

Each time I get this output:

npx: installed 1 in 2.775s

I've verified that the webpack command exists in my ./node_modules/.bin directory where npx is looking. Can anyone think of why it's downloading webpack every time? It can take up to 7 seconds to complete this step, which is slowing down my builds.

Margrettmarguerie answered 15/3, 2018 at 14:44 Comment(4)
I think the "installed 1" is not about webpack but due to a weird bug which makes it install a package "prefix" every time you run npx. It's a weird bug: github.com/zkat/npx/issues/148Machzor
that kills npx for me..Squarerigged
solution is to install npm-run globallySquarerigged
I cannot reproduce on npx 6.14.11 after npm install webpack;npm install webpack-cli;npx webpack it runs immediately without redownload. Or do you want to not install webpack with npm install? Can you provide versions of everything if your behavior differs?Osugi
C
4

Old answer:

npx doesn't reuse previously installed packages, instead it pulls down that package's dependencies every time that you run it.

Update on 06 May 2022 for newer versions of npx e.g. ver. 8.3.0:

Now npx does use previously installed packages without need to reinstall anything! Looks like npm team fixed old issue some time ago, not sure which version was first that received this fix.

npx allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm run.

https://docs.npmjs.com/cli/v8/commands/npx

Coenzyme answered 31/10, 2019 at 11:36 Comment(3)
A note for other beginners, on npx 6.14.11 when I do npm install --save vaca;npx vaca the execution does not redownload. It only redownloads every time if I don't do the npm install such that vaca is not in node_modules. Either this answer meant to address only the case of "from an arbitrary directory", or npx changed since.Osugi
@Maksim, Could you please give a reference to this information? Thanks!Overabundance
I updated my old answer with new information. Thanks for pointing me out to the changes.Coenzyme
V
1

I agree with laggingreflex. It was probably not webpack that's being installed in your case each time. Refer this issue, it is of around the same time. I don't think it is applicable to newer versions.


The other answer is misleading, or at least I don't quite understand its context.

Quoting the readme:

Executes <command> either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for <command> to run.

By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that. If <command> is not found, it will be installed prior to execution.

Quoting release blog:

npx has basically no overhead if invoking an already-installed binary  —  it's clever enough to load the code for the tool directly into the current running node process!

Calling npx <command> when <command> isn't already in your $PATH will automatically install a package with that name from the npm registry for you, and invoke it. When it’s done, the installed package won't be anywhere in your globals, so you won’t have to worry about pollution in the long-term.

Although the above referenced npx as a separate package is now deprecated and npx is now a part of npm-cli, but the essence is still same, and can also be verified by the official docs:

This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely).

If any requested packages are not present in the local project dependencies, then they are installed to a folder in the npm cache, which is added to the PATH environment variable in the executed process.

Visitation answered 17/7, 2021 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.