Want to check Node version before project get executed using script!!!

Codeptive Solutions
CodeptiveSolutions
Published in
4 min readSep 30, 2019

Hello Everyone!!! In this post, we will learn how to check Node version of a system using a script and if the system’s Node version is not compatible with our project’s required Node version then forcefully stop npm package installation(npm install) as well as project startup command(npm start) and notify developers about this issue.

We will get below output at the end of the story.

output of the story

Problem

Sometimes our projects are depended on a specific Node version, If developers didn’t install that specific Node version then they might face some issues.

E.g. Node Async/Await. If we used Async/Await in our projects then we need Node 8+ version otherwise it wouldn’t work as expected.

So how our project team especially developers will know which Node version they should install and start the development? Let’s discuss its solution!

Solution 1: Manual Solution but such a pain…

We have to tell each and every team member that please use this Node version otherwise, it won’t work properly. So the problem is we need to tell everyone ourselves manually.

Solution 2: Use common approach but not getting satisfaction…

  • People generally use engines in package.json file to specify required Node Version.
{ 
"engines" : {
"node" : ">=8.0.0 <11.0.0"
}
}
  • People often use .nvmrc to specify Node version.
  • People often describe which Node version required for the project in README.md file

But the problem in the above approaches is if developers didn’t install project required Node version, still developers could be able to install project dependencies via “npm install” and could go further. It doesn’t check forcefully.

So why we don’t make a script which solves our problem. That’s a cool idea, Isn’t it!!

Jackpot Solution: Check with our own custom script

So first of all, Let’s write a script for check Node Version of system.

Step 1 :

Create one JavaScript file in root directory like checkNodeVersion.js

const result = process.versions;if (result && result.node) {  if (parseInt(result.node) >= 8) {    console.log("\x1b[47m\x1b[32m%s\x1b[0m", "-------******* Good to Go with your Node Version: " + result.node + " *******-------");  } else {console.log("\x1b[47m\x1b[31m%s\x1b[0m", "-------******* Package installation(npm install) or Project startup command(npm start) failed due to Node Version, Please install and use Node Version >=8 *******-------");    console.log("\x1b[47m\x1b[33m%s\x1b[0m", "-------******* Your current Node Version is: " + result.node + " *******-------");    process.exit(1);  }} else {console.log("\x1b[47m\x1b[31m%s\x1b[0m", "-------******* Something went wrong while checking Node version *******-------");  process.exit(1);}

NOTE: In console.log(), %s is wherein the string (the second argument) gets injected. \x1b[0m resets the terminal color so it doesn’t continue to be the chosen color anymore after this point.

For More Colors Reference:

// General settings:
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
// Foreground colors:
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
// Background colors:
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"

You can modify the logic of script based on your requirements. It’s totally up to you.

Step 2 :

Now our script is ready, we just need to find the place where we can inject.

We will use the preinstall as well as prestart script of npm so that our custom “checkNodeVersion” script runs before execute npm install as well as npm start.

You can find more places on https://docs.npmjs.com/misc/scripts.

So update your package.json file like

{
"name": "...",
"version": "...",
"scripts": {
"preinstall": "node checkNodeVersion",
"prestart": "node checkNodeVersion",
...
},
"dependencies": {
...
},
"devDependencies": {
...
}
}

Now If the developer’s system existing Node version are matched with our script then it will install dependencies and able to go further otherwise it will stop the execution and skip the dependencies installation and notify developers about this Node version issue through our script & messages.

That’s it!! Now when every developer tries to execute “npm install” OR “npm start” which every developer should do in every project, they will go through our checks then and then it will go further.

--

--

CodeptiveSolutions
CodeptiveSolutions

Published in CodeptiveSolutions

Codeptive Solutions is community to help other people around the world about programming, technology, coding tricks, etc. We are working as remote freelancer to help people to achieve their dreams, business.

Codeptive Solutions
Codeptive Solutions