Volta homepage says: "The JavaScript Launcher ⚡". What it does for me is:
- Have multiple
node
andyarn
versions installed side by side - Switch between individual
node
andyarn
versions seamlessly (cd
into the directory containingpackage.json
is enough) - Make sure that everyone in the team uses the same version of
node
andyarn
- Globally install
node
modules
How to volta
Install:
curl https://get.volta.sh | bash
volta install node
## That's it!
Pin node
and yarn
versions in your project:
cd my-project
volta pin node@10.18.0; volta pin yarn@1.21.1
Now you will have the respective versions of node
and yarn
available every time you enter the my-project
directory.
Volta in Docker
The problem with Docker
is that volta
relies on sourcing the scripts stored ~/.bashrc
. You could in theory prepend every "node
command" with source ~/.bashrc &&
or something, but that's not very practical. Following example Dockerfile
shows my solution with explanation on individual lines:
FROM debian:stable-slim
WORKDIR /myapp/
# curl and ca-certificates are needed for volta installation
RUN apt-get update \
&& apt-get install -y \
curl \
ca-certificates \
--no-install-recommends
# bash will load volta() function via .bashrc
# using $VOLTA_HOME/load.sh
SHELL ["/bin/bash", "-c"]
# since we're starting non-interactive shell,
# we wil need to tell bash to load .bashrc manually
ENV BASH_ENV ~/.bashrc
# needed by volta() function
ENV VOLTA_HOME /root/.volta
# make sure packages managed by volta will be in PATH
ENV PATH $VOLTA_HOME/bin:$PATH
# install volta
RUN curl https://get.volta.sh | bash
RUN volta install node
# test whether global installation of packages works
RUN volta install ember-cli
# test that volta manages node/yarn version
COPY index.js package.json yarn.lock /myapp/
RUN yarn --pure-lockfile
Now let's have package.json
:
{
"name": "volta-in-docker-example",
"volta": {
"node": "10.17.0",
"yarn": "1.20.0"
}
}
If you build such Docker
container:
docker image build -t volta:0.0.1 -f volta.Dockerfile .
And then check the node version installed, you should get whatever was specified in package.json
:
docker container run --rm --name volta volta:0.0.1 node --version
# v10.17.0
The why
One could put FROM node:10.17.0
directly into the Dockerfile
. But:
- One might not want to run
node
projects locally in a container, thus there will be a need to sync the versions - Dev environment synchronisation is hard & important & hard
-
yarn
versions should also be in sync to prevent possible (real) problems
Top comments (4)
7 0.316 /bin/bash: volta: command not found
executor failed running [/bin/bash -c volta install node]: exit code: 127
Did you find solution?
If you're using alpine it will fail. See github.com/volta-cli/volta/issues/473
I didn't, reverted to using a base node image for now.
Still investigating though.