Note: currently only works on *nix systems (until a powershell script can be created)
With the technique below, you can run node
, npm
, npx
, or yarn
commands as if the programs were installed
natively on your system, and you won't even know the difference! This includes any ports that your app or dev process
will start up and use for development, as well as compatibility with persistent npm config --global
cli usage.
See more in the Usage section below.
Setup is simple, merely put the node script on your PATH, symlink npm/npx, reload your shell, and you're ready to code!
- save the
node
file below to a spot on your user PATH chmod +x node
to make sure the file is executable.- Verify installation using
which node
ornode --version
- Create symlinks on your path for any command you wish to use in the node container (
npx
,npm
,yarn
)
To demonstrate, let's assume you are on a brand new machine, and haven't modified your PATH at all yet.
The commands below will walk you through the steps above by creating a .local/bin
directory for user scripts,
and add it to your PATH by editing your shell's .rc file. Then we'll start at step 1, and go through step 4.
note: in the example below substitute ~/.bashrc
for whatever your shell's config file is
Pre-Steps (create a local PATH destination for the script)
mkdir -p ~/.local/bin
echo "export PATH=\$HOME/.local/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
Now steps 1 - 4
curl -LSs https://gist.githubusercontent.com/LongLiveCHIEF/039c8039c5e4147bb1ca5e0704588cc0/raw/d5c896cd0780ef820dc321f14ee0cc53164759f1/node -o ~/.local/bin/node
chmod +x ~/.local/bin/node
ln -s $(which node) ~/.local/bin/npx
ln -s $(which node) ~/.local/bin/npm
Verify installation
$ node --version
v13.2.0
$ npm --version
6.13.1
$ npx --version
6.13.1
By default, this will utilize the latest
node image. You can change the version of node container being
used by manipulating the NODE_VERSION
env variable used. If the docker image for the version you specify
isn't available locally, it will first pull the image before executing the command.
There are 3 techniques for this:
single run
Prefix the call of the node
command with the NODE_VERSION
var like so:
$ NODE_VERSION=14 node --version
v14.1.0
For entire shell session
$ export NODE_VERSION=12
$ node
Welcome to Node.js v12.16.3.
Type ".help" for more information.
>
Set a permanent default
This is done by setting or updating the NODE_VERSION
variable exported in your shell's config file:
$ echo "export NODE_VERSION=12.13.1" >> ~/.bashrc"
$ source ~/.bashrc
$ node --version
v12.13.1
This is really handy!
Any idea how to make it work with additional packages that also make use of init commands?
for example installing vue-cli via npm then running
vue create hello-world