Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check node and npm versions before a npm install?

I want to only allow certain version of node and npm before the user can run his npm install on my module.

In the NPM documentation, there is an engine attribute, dedicated to this task:

  "engines": {
    "node": ">=4.0.0",
    "npm": ">=3.0.0"
  }

These parameters will only allow node and npm versions up to 4 and 3.

However, the documentation says that the engine-strict attribute should be enabled in order to check the versions. On engine-strict, it is said that:

This feature was deprecated with npm 3.0.0

So, is there a way, with npm3, to define minimal Node and NPM versions for a module?

like image 221
Romain Linsolas Avatar asked Sep 20 '25 22:09

Romain Linsolas


1 Answers

Yarn checks the engine out of the box at the time of installation. But with npm, you may have to write a custom validator at this moment for checking node versions before firing a script.

export function verifyNodeVersion(version) {
  const nodeVersion = process.versions.node;
  if (!nodeVersion.startsWith(`${version}.`)) {
    throw new Error(
      `Incorrect version of Node found: ${nodeVersion}, Expected: v${version}.x`
    );
  }
  console.log('Current Node version:', nodeVersion);
  return true;
}

And call the function in the entry points.

like image 146
Abhinaba Avatar answered Sep 22 '25 13:09

Abhinaba