These are two different cli tools as part of your nodejs installation. Their goal is to make our work with JavaScript packages easier. The main difference is that npm is the actual package manager, but npx is used to execute the JavaScript packages.
NPM means Node Package Manager, this is the default package manager for nodejs. If you have installed nodejs in your machine, then you will have available npm in yours command line\cli (terminal, dos). It interact with the online database (NPM Registry) of npm so to use it you should have available internet connection. This online database has private and public repositories to fetch from. Anybody can publish are package in this repository.
NPX means Node Package eXecute this is very powerful tool and npm package runner. This cli tool installed with your npm allows you to execute any JS Package which is available in the npm registry for which i mentioned above, without installing it.
To test if you have npx installed on your machine, just open your terminal and write
npx -v
This will show your current installed version of npx if is available, if you see error that the command is not found, just install the tool with
npm install -g npx
-g attribute will install the npx globally and you will have the command from each directory and new open terminal window in your system. NPX is mostly used when wants to run some JS Package only once.
As i tell you above, npm can download packages to yours machine from the public repository of npm registry.
If you run
npm install package-name
or
npm i package-name
the package will be downloaded to the current directory from which the terminal is opened. With the -g attribute you can install it globally.
If you wants to run some package, without downloading it, you can just type
npx package-name
For example, if you wants to install tailwindcss, you can do it with
npm install -D tailwindcss
then if you wants your tailwindcss package to make automatically configuration for you, just type
npx tailwindcss init
In this example, the npx command will run the tailwindcss script which will create the tailwind.config.js configuration file.