Does npx look for globally installed packages?
Asked Answered
C

2

13

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?

Cob answered 11/6, 2018 at 18:55 Comment(3)
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 globalUsher
chekc this link for more details - blog.scottlogic.com/2018/04/05/npx-the-npm-package-runner.htmlUsher
The benefit of npx is that you don't need to use -g anymore.Virgate
U
4

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

Usher answered 11/6, 2018 at 19:46 Comment(5)
@Gorey is saying that it look into global.Cob
Your answer is unclear, @Gorey is right. check out npmjs.com/package/npx "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"Amorita
@red , not sure what is unclear, i gave same information with reference link, did you check that link?..and not sure for downvoteUsher
where does npx install the package? does it install at global scope or local to the project?Metamer
Note: When npx installs the package, I found that it just installs it in the npm cache and runs it from there. It does not install it locally nor globally.Bulkhead
G
11

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.

Gorey answered 11/6, 2018 at 19:58 Comment(3)
This should be the right answer. If the package is available locally it will take it, otherwise it falls back to the global one.Plop
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.Perdurable
@Perdurable When using the -p opton we need to follow this format: npx [options] [-p|--package <package>]... <command> [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)Unconditioned
U
4

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

Usher answered 11/6, 2018 at 19:46 Comment(5)
@Gorey is saying that it look into global.Cob
Your answer is unclear, @Gorey is right. check out npmjs.com/package/npx "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"Amorita
@red , not sure what is unclear, i gave same information with reference link, did you check that link?..and not sure for downvoteUsher
where does npx install the package? does it install at global scope or local to the project?Metamer
Note: When npx installs the package, I found that it just installs it in the npm cache and runs it from there. It does not install it locally nor globally.Bulkhead

© 2022 - 2024 — McMap. All rights reserved.