24

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.

serverpunk
  • 10,665
  • 15
  • 61
  • 95

2 Answers2

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

Maksim Shamihulau
  • 1,219
  • 1
  • 15
  • 17
  • 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. – Ciro Santilli OurBigBook.com Apr 22 '21 at 19:07
  • 1
    @Maksim, Could you please give a reference to this information? Thanks! – ddsultan Dec 01 '21 at 11:49
  • I updated my old answer with new information. Thanks for pointing me out to the changes. – Maksim Shamihulau May 06 '22 at 21:29
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.

brc-dd
  • 10,788
  • 3
  • 47
  • 67