If you ever run npm install
or yarn
and face with a compile error because node version, you are not alone. This is usually the case for me because I use node-sass
often to SASS for styling. This library provides bindings for Node.js to Libsass. However, each version of node-sass supports a different version of node.
Sometimes you leave projects alone and not make changes for long time. Next time you visit, you get an error because your local node version is now different.
There are 3 solutions that I could think for this problem:
Nvm is a version manager for node. This will be useful at testing node versions for the examples. Follow the instructions on nvm site to install if you don't already have it.
This is the simplest way to specify node version to use in a project. However, it's often forgotten.
Node version can be specified in the package.json with engines
property.
You can specify the version of node that your stuff works on.
— npm documentation
This property can be set to an exact node version or a range can be provided such as >=10.0.0 <11.0.0
.
Something to be mindful is that, this will only be checked when hte project dependencies are getting installed, not when it's build.
*Examples are using yarn but same will work with npm too.
mkdir testing-node-versions && cd testing-node-versions
yarn init
engines
to the package.json
file and set to be between node version 10 and 11.{
"name": "Node versionsta",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"engines": {
"node": ">10.0.0 <11.0.0"
},
"dependencies": {}
}
nvm install 12.0.0
nvm use 12.0.0
yarn
commandThe engine "node" is incompatible with this module. Expected version ">10.0.0 <11.0.0". Got "12.0.0"
nvm install 10.0.1
nvm use 10.0.1
yarn
yarn install v1.16.0
[1/5] 🔍 Validating package.json...
[2/5] 🔍 Resolving packages...
[3/5] 🚚 Fetching packages...
[4/5] 🔗 Linking dependencies...
[5/5] 🔨 Building fresh packages...
✨ Done in 1.86s.
This can help you ensure project is installed with a node version that it supports. You can also specify npm or yarn versions here too.
However, if you have to set the node version manually for the project after you see the error. Could we make this automatically?
You can include a .nvmrc
config to your project to help nvm set node version for you. When you run nvm use
, it will automatically pick up the version from this config file and use it if it is already installed. Otherwise it will give an error like this:
N/A: version "5 -> N/A" is not yet installed.
This solution expects developer to already have installed nvm in their machine and also you need to nvm use
before installing the project. Althought nvm documentation provides with a script that you could add to your bash profile to do this automatically.
But there are more than one node version manager such as n, avn and fnm. What if your machine is using another one?
You can create a docker container to install dependencies for your project. Install docker engine following the instuctions on docker site.
Lets create the docker-compose.yml
file
version: "3"
services:
node:
image: node:10-alpine
volumes:
- ./:/app
working_dir: /app
command: ["yarn"]
node_modules
as volume. It will bind the current directory ./
of host to app
directory of the container.yarn
as command. Thiw specifies the command we want to run by default when container is run.docker-compose run node
.$ docker-compose run node
yarn install v1.16.0
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 23.08s.
Dependencies are installed using the node version on the container. You don't need the container the run. Installing dependencies is all we need.
This is only the start. You can create more commands to building and testing application. Combining this with Makefile can automate and standardize alot of the tasks.