What does the npx command install? - node.js

This question is about understanding how the npx command works and what it exactly does under the hood. (The documentation does not really help - see below)
From the documentation:
Installation-less command execution
There is another great feature of npx, which is allowing to run
commands without first installing them.
Great. Let's try running some command without installation:
npx node#6 -v
And in result one gets:
Need to install the following packages:
node#6
Ok to proceed? (y)
So there is anyway installation going on. And one could see that something gets installed with npm cache ls.
If we run the command npx node#6 -v again, there won't be a prompt asking for installation. That means the installation (that didn't happen in the first place) persists. And now one could execute the command even without connection to the internet/npm.
For a person who does not know all nuts and bolts of the npm it is kind of strange to find "installation-less execution", which asks for installation.
Could some one give more logical explanation like: when you execute npx node#6 -v this and that happens under the hood, this package will be downloaded and unpacked there ... then executed ... then deleted/or persists for so long ... and so on.
What if I run npx package today (and it gets "installed") and I run the command in 3 months? Would npx check if there is a newer version or would it call the "installed" one?
Is there a documentation on that?

Related

NPM won't automatically set up cmdlet's for me?

this is the first this ever happened to me.
I am trying to install a documentation generator (docma) and for some reason I follow everything they say in the docs and can't execute 'docma' or 'docma serve', it says it's not recognized as a cmdlet, tried it on my Linux machine and it also isn't recognized as a command, so there's no way for me to serve my documentation and the developer stopped supporting that years ago.
Thanks in advance
EDIT: I should add that I don't get a node_modules folder
If you don't have a package.json file for your project, create one:
npm init
(If you don't care about what the contents of the package.json file are, then you can accept all the defaults with npm init -y rather than answer the questions the command will ask you.)
The documentation says to install docma in a project like this:
npm i docma -D
Do that. That will create a node_modules directory if you don't already have one.
From there, all the commands in the docma documentation can be executed from the command line if you precede them with npx. So where it says docma, you can use npx docma. And where it says docma serve, you can use npx docma serve. (You will need to be in your project directory for this to work!)
Another solution would be to install docma globally (npm i docma -g) but that has its own risks and complications (such as requiring elevated privileges in some situations). What I've described above is (IMO) safer and better.

NPM command not found after installing Node via PKG (no brew)

I tried to remove NPM and Node and reinstall them both. I'm a complete noob in the process as you'll notice.
I sadly can't remember the commands I used.
After I removed them, I ran node -v and it returned command not found and I ran npm -v and it returned command not found, so I thought the process was successful.
I then went to Nodejs website and downloaded the latest package for MacOS (v12.11.1) and ran the install. It said it was successful. I check node -v and it returned 12.11.1, as expected, but then I checked npm -v and it sadly returned npm not a command!!
If I run which node I get /usr/local/bin/node. If I run which npm I get nothing returned.
I've since tried lots of ways of removing them both again just in case something was missed but I get the same thing every time - no NPM!
Most of the answers on StackOverflow talk about Brew but I have not used Brew and I have tried so many of the other answers but just can't get NPM back.
The PKG installer mentions this: 'Make sure that /usr/local/bin is in your $PATH.' but not entirely sure what that means and if that's my problem or not.
Does anybody know a surefire way to remove all traces of both, so when I reinstall the PKG from Node, I get both Node and NPM installed and working correctly?
UPDATE:
I seem to have had some success since posting this question. I followed instructions at https://docs.npmjs.com/misc/removing-npm.html to manually remove 'everything NPM-rleated' and ran the installer again. Now when I check npm -v it returns to the latest version number 6.11.3.
I will test things a bit more before deleting/answering my own question.
To fix this problem I ran the following command as suggested by NPM:
To remove everything npm-related manually:
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
All was OK after I reinstalled Node and checked NPM's version using npm -v.
Googling 'Adding to $PATH mac' returns this as the top result:
https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/
Open up Terminal and run the following command:
sudo nano /etc/paths
Enter your password, when prompted.
Go to the bottom of the file, and enter the path you wish to add.
Hit control-x to quit.
Enter “Y” to save the modified buffer.
That’s it! To test it, in new terminal window, type:
echo $PATH
this is what I did on mac
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
brew uninstall node
brew install node

npx requires re-installation of packages every time

Currently using npx as a workaround for packages installed globally by npm as commands can never be found despite all attempts at exporting to the correct path.
npx works in finding the command but has to reinstall the package every time to find it which means multiple uses of the same package runs into problems.
I've tried using the npx --no-install but then npx runs into the same problem as I did with "command not found" being the result.
All this is attempted through the ubuntu terminal.
Solved my issue. The npx workaround was never going to work when my npm pathing was acting up. Had to reinstall npm (again) and manually create a global directory rather than use the default. Then had to export that path variable and append it to .bashrc confirm it with "source /.bashrc" and then restart the terminal. On restart everything worked fine and had no further problems with npm installs.

Exercism exercise: jasmine-node . - command not found

I'm trying to run the first exercise in Exercism. I have followed the readme instructions and installed Homebrew, and then installed the CLI. Everything went fine.
I then typed in the command line to download the first test - Hello World. Again, this worked fine.
The next instruction was
Execute the tests with:
jasmine-node .
I assume this is to be typed into Terminal? This is what I've tried and it says 'command not found'. There is no other instructions. I've never even come across Jasmine before. I downloaded jasmine-node separately via npm,
npm install jasmine-node -g
...but this has still made no difference.
Can anyone suggest where I am going wrong? Thanks.
I think you are looking for this link
http://exercism.io/languages/javascript/installing
It details all the dependencies you need to install
the command that did it for me was
npm install jasmine-node -g

NPM throws errors at traceur install on OS X

https://github.com/tejas-manohar/itnerary-civic-hacking -- I cloned this git repo down locally. Installed grunt-cli globally, and ran npm run nss (script written by author of original server template -- look in package.json, not complex) and faced numerous errors at the traceur install + git cloning. I've included the trail from terminal window in the pastebin linked below. The npm debug/error log mentioned is not present at the suggested location. npm install alone does not help the situation and returns nothing back. NodeJS v0.10.29 is installed via Node Version Manager (NVM) on OS X 10.9.3.
http://pastebin.com/UJFL3k2E
I'm not the most adept with some of the technologies discussed here. Would someone please attempt to walk me through (a) solution(s) and/or steps to get better clues? All assistance is appreciated.
There's a couple of problems going on which are causing the problems you have seen. The first of which is the command that is run when you run npm run nss, which fails for you with this:
rm: ../../app/static/js/vendor/traceur.js: No such file or directory
This is because, well, the file simply does not exist (yet). If you look at the commands which are run when you run npm run nss, you'll see that the remove file is followed by the copy file of traceur.js to that very location. So I would guess that whoever wrote the script intended for this to be run after it was initially setup, and the remove/copy would be done once the file was initially copied there. Therefore, you must first copy the traceur.js file to app/static/js/vendor before you can successfully execute npm run nss.
But then the next problem, the file should be copied from tools/traceur-compiler/bin doesn't exist either. This is because the traceur-compiler project has recently deleted this (compiled) file from their project, which you can see via this commit: https://github.com/google/traceur-compiler/commit/429c3d850dcb7636320e81fd782c61a06de0fbf1
So you need to regenerate this file, which you can do by (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor
This will get you in the same state as if you successfully ran npm run nss.
You won't be able to re-run the npm run nss command, but you really shouldn't need to since its more of a setup command than anything (it creates a directory, clones a github repo, etc). If you wanted to update the traceur-compiler in the future (though you may never have to do this) then I would recommend running the following steps (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
git pull origin master
npm install
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor

Resources