Skip to content

Instantly share code, notes, and snippets.

@LongLiveCHIEF
Last active April 13, 2023 12:25
Show Gist options
  • Save LongLiveCHIEF/039c8039c5e4147bb1ca5e0704588cc0 to your computer and use it in GitHub Desktop.
Save LongLiveCHIEF/039c8039c5e4147bb1ca5e0704588cc0 to your computer and use it in GitHub Desktop.
Run node, npm, npx, yarn as container

Developing with Node, without installing node!

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!

Setup

  1. save the node file below to a spot on your user PATH
  2. chmod +x node to make sure the file is executable.
  3. Verify installation using which node or node --version
  4. 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

Usage

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
#!/bin/sh
set -e
tag="${NODE_VERSION:-latest}"
user=$(id -u)
group=$(id -g)
scriptname=`basename "$0"`
set -- $scriptname "$@"
docker container run -it --rm \
--user $user:$group \
--network host \
--volume $HOME/.npmrc:/home/node/.npmrc \
--volume $PWD:/usr/src/app \
-w /usr/src/app \
node:$tag "$@"
@Fanna1119
Copy link

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

@LongLiveCHIEF
Copy link
Author

@Fanna1119 2 options for that

  1. Just use npx vue create hello-world (or npx vue <command> [options]
  2. take the same approach as in the README above, except instead of using the node image, you use your own custom node image that includes vue, then add an additional symlink for vue: ln -s $(which node) ~/.local/bin/vue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment