npm is a Package Manager tool mainly used to install packages.
npx is a Package Runner tool used to execute packages (that are installed locally).
There are two ways you can install node packages. Locally (local to the project) or Globally (in the user environment).

Installing a package locally

A locally installed package will be installed into the current working directory.
A sub folder called "node_modules" will be created that contains the locally installed package (or node modules).
You can use the npm command to install a package locally.

npm install package-name 

When we want to run our package we use either of these commands:

node ./node_modules/package-name/bin/package-name 

Alternatively you can add a script entry in the package.json file

scripts : { 
    "build": "package-name"

If a script has been created then you can run the package using:

npm run build 

Installing a package globally

A globally installed package is installed in the user profile /<User>/local/lib/node_modules folder.
You can use npm to install a package globally using the --global command line switch

npm install --global package-name 

If a package has been installed globally you can run the package using:

node package-name 

Running with NPX

npx is a replacement for installing global packages.
npx installs packages locally, but allows you to run them as if they were global.

npx package-name 

If the package is already installed then this will run the package.

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext