如何在脚本中要求最低版本的node.js?

时间:2022-04-18 19:42:34

I just found out that a script I wrote only works on node 0.10 because it uses readable events.

我刚刚发现我编写的脚本只能在节点0.10上运行,因为它使用可读事件。

How do I require a minimum version of node.js in my script so that users know that they need to upgrade?

如何在我的脚本中要求最低版本的node.js,以便用户知道他们需要升级?

2 个解决方案

#1


12  

In package.json:

{ "engines" : { "node" : ">=0.10.3" } }

From the docs.

来自文档。

Edit, a programmatic way:

编辑,一种编程方式:

var pkg = require('./pacakge'),
    semver = require('semver');

if(!semver.satisfies(process.version, pkg.engines.node)) {
  // Not sure if throw or process.exit is best.
  throw new Error('Requires a node version matching ' + pkg.engines.node);
}

#2


1  

Add this to top the top of your script.

将其添加到脚本顶部。

var versionComps = process.versions['node'].split('.');
if (parseInt(versionComps[0]) === 0 && parseInt(versionComps[1]) < 10) {
  console.log('Script requires node.js version >= 0.10');
  process.exit(1);
};

#1


12  

In package.json:

{ "engines" : { "node" : ">=0.10.3" } }

From the docs.

来自文档。

Edit, a programmatic way:

编辑,一种编程方式:

var pkg = require('./pacakge'),
    semver = require('semver');

if(!semver.satisfies(process.version, pkg.engines.node)) {
  // Not sure if throw or process.exit is best.
  throw new Error('Requires a node version matching ' + pkg.engines.node);
}

#2


1  

Add this to top the top of your script.

将其添加到脚本顶部。

var versionComps = process.versions['node'].split('.');
if (parseInt(versionComps[0]) === 0 && parseInt(versionComps[1]) < 10) {
  console.log('Script requires node.js version >= 0.10');
  process.exit(1);
};