Finds the bin
directory where node
, npm
and npx
are installed.
Or if not found, installs a local copy of them in a temp folder, then
outputs where they are.
If no Node.js installation is found, the script makes use of nvm, configuring it to install the latest Node.js LTS version in a temporary directory.
curl
orwget
; andtar
orunzip
orgit
curl -o find-node-or-install \
https://raw.githubusercontent.com/hugojosefson/find-node-or-install/master/find-node-or-install
chmod +x find-node-or-install
If you want, you can change the version of Node.js to be installed. Do
it by setting the NODE_VERSION
env variable. Default is --lts
.
Example:
NODE_VERSION=12 ./find-node-or-install
export PATH="$(./find-node-or-install):$PATH"
Put this at the top of your Makefile
:
SHELL := /bin/sh
NODE_PATH = $(shell ./find-node-or-install)
PATH := $(NODE_PATH):$(shell echo $$PATH)
All of this enables you to use any npm module in an environment where you don't have to make assumptions about Node.js/npm availability.
There are also some executables+symlinks available here. They can be
used to launch node
, npm
, npx
or yarn
via
find-or-install-node
.
If you put these comments at the top of a .js
file, you can make it
self-contained and executable:
#!/bin/sh
/* 2>/dev/null
url=https://raw.githubusercontent.com/hugojosefson/find-node-or-install/master/find-node-or-install
PATH="$( (curl -s $url||wget -qO- $url) 2>/dev/null|sh 2>/dev/null):$PATH"
exec node "$0" "$@"
*/
console.log('Hello from inside a self-contained, *magically* executable .js file!')
To keep find-node-or-install
downloaded and offline:
curl -o find-node-or-install \
https://raw.githubusercontent.com/hugojosefson/find-node-or-install/master/find-node-or-install
curl -o node \
https://raw.githubusercontent.com/hugojosefson/find-node-or-install/master/node
chmod +x find-node-or-install node
ln -s node npm
ln -s node npx
ln -s node yarn
Then in a hello.js
file which you want to execute, in the same
directory:
#!./node
console.log('Hello from inside an executable .js file!');
Make it executable, and run it:
chmod +x hello.js
./hello.js
=> Hello from inside an executable .js file!
MIT