9

I am using Node.js 10.1.0 and npm 6.0.0.

I have installed a package with npm install -g example-package,

Will npx look for it? What about npx -p example-package, does it only look on npm registry?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • npx looks in local /node_modules folder for the example-package and if it is not available , it downloads and runs but it doesnt look for global , as it is kind of replacement for global – Naga Sai A Jun 11 '18 at 19:03
  • chekc this link for more details - https://blog.scottlogic.com/2018/04/05/npx-the-npm-package-runner.html – Naga Sai A Jun 11 '18 at 19:03
  • The benefit of `npx` is that you don't need to use `-g` anymore. – zero298 Jun 11 '18 at 19:05

2 Answers2

9

In Node.js v10 (npm@6 and probably later);

npx will look global binaries, after looking locally.

But we can use -p option to prevent looking globally, like:

npx -p name_of_module

Note npx is an npm package runner that executes a <command> (e.g. npm package binaries) by FIRST looking in local node_modules/.bin directory.

So even if we remove it from package.json, as long as binary exists in node_modules/.bin, npx will continue using local.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
bluelovers
  • 1,035
  • 2
  • 10
  • 22
  • This should be the right answer. If the package is available locally it will take it, otherwise it falls back to the global one. – millsp Apr 13 '21 at 18:49
  • Not sure this is still working: `npx -p jest --version` gives me 6.14.15 which is my current npm version, not jest version. `npx jest --version` gives me 27.3.1 which is accurate. – jcollum Jan 27 '22 at 22:05
  • @jcollum When using the `-p` opton we need to follow this format: `npx [options] [-p|--package ]... [command-arg]...`. So, it looks like `npx -p jest jest --version` will do the right thing. I guess this is to support packages where the command name is not the same as the package name (e.g. `typescript` package has `tsc` as main command) – Paul Huynh May 23 '22 at 00:07
2

NPX included in NPM 5.2 which looks in your local/node_modules folder to avoid version mismatch with the globally installed package version

If package is not available, npx will automatically install npm packages and it will not be looking for globally installed packages

Check this link for reference - https://blog.scottlogic.com/2018/04/05/npx-the-npm-package-runner.html

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
  • @bluelovers is saying that it look into global. – Dimitri Kopriwa Jun 12 '18 at 06:35
  • Your answer is unclear, @bluelovers is right. check out https://www.npmjs.com/package/npx "By default, npx will check whether exists in $PATH, or in the local project binaries, and execute that. If is not found, it will be installed prior to execution" – redben Dec 04 '18 at 08:02
  • @red , not sure what is unclear, i gave same information with reference link, did you check that link?..and not sure for downvote – Naga Sai A Dec 04 '18 at 15:59
  • 1
    where does `npx` install the package? does it install at global scope or local to the project? – prgmrDev Dec 14 '21 at 18:13