If I'm using npx to run a binary as a one-off, it'll output the following:
npx my-module
/// npx: installed 1 in 1.34s
/// Hello, from my module!
Where are these binaries stored by default? Does npx save the binaries after execution, a-la npm or does it just run them and then remove the files?
It's my understanding that npx will first look in the local node_modules/.bin diectory and then the /usr/local/bin directory, before it downloads the module. But I've checked both of those locations and don't see the new module...
npm version 7 will cache the packages in a _npx directory. It has a cache layout that apparently involves a hash. For example, for me npx shellcheck installs the executable in ~/.npm/_npx/cca5ebdff9ce100b/node_modules/.bin/shellcheck. (Note the cca5ebdff9ce100b.) However, I very much doubt that the algorithm can be relied upon to be consistent across versions of npx.
The whole point of npx is that you can run the packages without installing them somewhere permanent. So I wouldn't use that cache location for anything. I wouldn't be surprised if cache entries were cleared from time to time. I don't know what algorithm, if any, npx uses for time-based cache invalidation.
To get the location from which npx runs the package, you can use -p to tell it to install a package and then use which <executable> or command -v <executable> to get the path. So, for example, what I did above to get the location of the shellcheck executable was to run npx -p shellcheck which shellcheck or npx -p shellcheck command -v shellcheck. Those commands assume a Linux or other UNIX-like operating system. I'm not sure what the equivalent for Windows would be.
$ npx -p shellcheck command -v shellcheck
Need to install the following packages:
shellcheck
Ok to proceed? (y)
/Users/trott/.npm/_npx/cca5ebdff9ce100b/node_modules/.bin/shellcheck
$
In Windows, this would be:
C:\Users\{YourUserName}\AppData\Local\npm-cache\_npx\
or the bash shell equivalent of:
~/Local Settings/npm-cache/_npx/ etc.
Related
My Problem
I have installed fnm (Fast Node Manager) from this github repo and it works all great except for installing global npm packages. For example, the well-known package nodemon is something I want installed globally and not im my node_modules project directory.
When installing the package globally there seems to be no problem:
And when checking the global package list, nodemon seems to be there:
But when running the command nodemon I get the following output:
As also seen in the fnm repository documentation there is a need to run this piece of code eval "$(fnm env --use-on-cd)"; on load in order to get fnm to work properly and this is what I have done in the .bashrc file.
Note
I am using windows 10, seems to be working on my mac laptop.
The Question
How can I have a global npm package installed for all or at least a single fnm node version? And what I mean by this, is that by running fnm use <NODE_VERION> you specify what node version to use as also seen in the repository documentation. I want to be able to run the nodemon command without it being installed in a project's node_modules directory.
You do not need to delete the multishells. The problem is the Git Bash path.
Fix is here: https://github.com/Schniz/fnm/issues/390
Put this in your .bashrc
eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH
if [[ -f .node-version || -f .nvmrc ]]; then
fnm use
fi
As mentioned this actually worked on my OS X machine (aka my mac book pro) but not on my windows 10 computer. The solution I came up with after analyzing thoroughly the behaviour of fnm is the following:
Go to C:\Users\<YOUR_USER>\AppData\Local\fnm_multishells and delete the directory if it exists.
When downloading global packages do it via CMD or any terminal which isn't bash (or the terminal that has the "$(fnm env --use-on-cd)"; script) as this makes fnm then search for the global package in the wrong place.
This approach mitigates any path errors as I found that this was the core problem. As shown in the screenshot above when trying to run nodemon it looks for it in C:\Program Files\Git\Users\Valeri..... but this directory simply does not exist. After removing the directory mentioned in step 1 fnm stops looking for nodemon in that path and instead uses the one installed via CMD.
Essentially, the "$(fnm env --use-on-cd)"; script allows us to use fnm properly but at the same time causes this issue. Simply download global npm packages from a terminal that does not run this command.
Edit
I just had the same issue and to confirm you don't even need to delete the fnm_multishells directory. Just run npm -g remove <whatever> and install it via cmd or powershell. A command-line which does not run "$(fnm env --use-on-cd)"; on load.
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?
My OS is win7, and I using MSYS2(version:MSYS_NT-6.1), Please give advice how to install nodejs and npm on this terminal, Thanks!
I found a solution for resolving the problem,
64bit env.
pacman -S mingw-w64-x86_64-nodejs
32bit env.
pacman -S mingw-w64-i686-nodejs
after installed, Open terminal
$ node -v
v6.11.0
As of 2020, the package mingw-w64-x86_64-nodejs is not available any more. The simplest way to have Node.js, npm and git installed on a Windows machine is using the official Windows installers:
Git: https://git-scm.com/download/win
Node.js (npm is shipped with it): https://nodejs.org/en/download/
After installation, open a command prompt (click on start, and then type cmd and [ENTER]) and verify that all three tools are there:
git --version
node --version
npm --version
Later on, to update Node.js, simply reinstall it from the same source.
I wasted a lot of time on this. My solution is:
Download the Windows Binary (.zip) from nodejs site (https://nodejs.org/en/download/current/)
Extract it to some folder
Add that folder to the PATH env variable
It does work to use the Windows installer, and Node.js helpfully provides bash-script versions of npm and npx in C:\Program Files\nodejs\ to help streamline the process.
However, contrary to Cerclanism's comment # jmgonet's answer, you should not use --full-path with MinGW, no matter what terminal you're using, since that will by default bring the entire Windows path into your MinGW environment.
(Assuming you're a typical Windows developer with things like MSVC, Windows Python, and etc. install dirs on your path, containing plenty of names that clash with MinGW path members, you can see how that might bite you at some point down the road. My full Windows CMD.exe %PATH% is 1236 characters! I don't want all that sucked into MinGW.)
Instead, you should add the nodejs install dir to your MinGW shell $PATH, say by using everyone's favorite ~/.profile/~/.zprofile $PATH-munging trick:
# Append node.js to path
case ${PATH} in
*"/c/program files/nodejs"*)
;;
*)
export PATH="$PATH:/c/program files/nodejs:"
;;
esac
You'll probably also want to set some configuration, since by default Windows npm will use ${APPDATA}/npm for prefix, ${LOCALAPPDATA}/npm-cache for cache, C:\Windows\system32\cmd.exe for shell, etc.
# To view the full config including all defaults and overrides
npm config ls -l
# To view the active config for the specified environment
npm config list -L {global,user,project}
Maybe I was just confused, but to me it seemed, from what the configs show/say, that setting prefix= in my user config would override even local installs. (The project-specific ones where you npm install without --global, directly into a node_modules subdir of the current dir.) But after testing, happily I can report that's not the case, so it's safe to override the builtin prefix= from your $HOME/.npmrc.
Whether or not you move the cache= or let it stay at C:\Users\<you>\AppData\Local\npm-cache\ is your call. I'm sure it'll work that way. (Well, maybe not from an MSYS shell, but from MinGW it should be fine.)
There are minor differences I haven't overcome, but the only one that comes to mind right now is:
npm help <command> opens a browser window to HTML documentation, instead of displaying man page content directly in the terminal like it does on Linux. (Makes sense, as I don't think the manpages are even installed on Windows. Still disconcerting, though.)
You can simply install nvm, then install nodejs through there. In your MSYS2 shell just run the following to download and install nvm. Its better to go directly here and copy the download commands as the version numbers will change in the url.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
then run the following to setup nvm on your bash path:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
After running those commands you can use nvm install 16 or whatever node major version number you want. Type just nvm to get a list of available commands.
I have this post-receive script as a git hook when exporting with the following contents
#!/usr/bin/env bash
export NODE_ENV=production
git --work-tree=/home/myusername/app --git-dir=/home/myusername/git checkout -f
cd /home/myusername/app
npm prune
npm install --production
knex migrate:latest
When ssh'd in my server I installed knex globally but it doesn't seem to exist within the bash shell's environment. I lack the knowledge of knowing how they are different. I also noticed my node versions were different. How do I import my user's normal environment?
To get the names and versions of modules that you have installed globally run:
npm list --global --depth=0
You can then install those modules on the second system with:
npm install --global module_name module_name ...
If you want to install a specific version of Node on the second system you can use nvm or see those answers for details:
Run npm as superuser, it isn't a good idea?
node 5.5.0 already installed but node -v fetches with "v4.2.1" on OS X & homebrew?
Node installed but node cannot be found in Ubuntu VPS
This is the first time I see a shebang like that:
#!/usr/bin/env bash
I've seen using /usr/bin/env to find node, python, perl interpreters etc. but never for bash. Usually it's just:
#!/bin/bash
Remember that if you want to run some program from a Bash script (without giving a full path) then it must be in a directory that is in the PATH environment variable.
To see your PATH run:
echo $PATH
Or you can use a full path to a binary in a script:
#!/bin/sh
/full/path/to/something
and then it doesn't need to be in the PATH.
I have a problem on npm installation
I have created a project say project A
cd ~/projectA
npm install sails
but sails command is not found after installation.
I know that it is successfully install in ~/projectA/node_modules directory.
but the executable cannot be sourced. And i know it is installed ~/projectA/node_modules/.bin
How can I source the .bin automatically whenever I enter into this projectA folder?
Did I did something wrong?
I can give you an inelegant solution, which is working for me. I just exported my path in my .bashrc file.
export PATH="$PATH:./node_modules/.bin"
Edit: It's been a while, but I have changed my strategy to use npm scripts instead. In the above case, setup package.json as follows:
"scripts": {
"sails": "sails"
}
Which you can then run with
npm run sails
or with arguments
npm run sails -- <args>
You should use the npm bin command to get an absolute path to your current node bin directory.
For example:
➤ lessc
bash: lessc: command not found
➤ npm bin
/home/brice/[...]/node_modules/.bin
➤ export PATH=$(npm bin):$PATH
➤ lessc --version
lessc 1.7.3 (Less Compiler) [JavaScript]
This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.
A bit more robust is:
export PATH=$(npm bin):$PATH
You can either run it, add it to your shell profile, or create an alias like:
alias snpm='export PATH=$(npm bin):$PATH'
If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!
To use on the command line like sails generate foo you will need to install the npm module globally.
npm install -g sails
You could also use the path to the bin in the command if you don't want to install globally:
./node_modules/sails/bin/sails.js generate foo
The official instructions for sails (https://github.com/balderdashy/sails) advises
To install the latest stable release with the command-line tool:
sudo npm -g install sails
This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH.
But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)
If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias".
npm install (locally) your beloved package (json-diff here, for instance)
cd ~ && npm install json-diff
alias it (save it in your ~/.xxxxrc file):
alias diffj "\`npm bin\`/json-diff !*"
Then, for diffing 2 json's:
diffj old.json new.json
In my ~/.bashrc, I have the following:
function nbin {
local dir;
dir=$(npm bin)
if [ -d "$dir" ]; then
( # subshell to not change this shell's $PATH
PATH=$dir:$PATH
eval "$#"
)
else
echo "\"$dir\" is not an npm binary directory." >&1
return 1
fi
}
I can then run executable foo in the .bin directory as:
nbin foo
Possible workaround with NPM 5.2+ using the npx command.
npx sails new test-project
See this question for a similar use case and elegant solutions.