Check for current Node Version

后端 未结 9 1874
温柔的废话
温柔的废话 2021-01-31 06:48

I need to programmatically access the current node version running in a library I am writing. Can\'t seem to find this in the docs.

9条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 07:20

    If you need to only check for the major version, you can use this quick-and-dirty snippet:

    const NODE_MAJOR_VERSION = process.versions.node.split('.')[0];
    if (NODE_MAJOR_VERSION < 12) {
      throw new Error('Requires Node 12 (or higher)');
    }
    

    Notes:

    • process.versions.node is easier to work with than process.version, as you do not have to worry about whether the version starts with a leading v.
    • If you still need to distinguish between ancient versions (e.g., 0.10 and 0.12), this will not work, as they will all be considered version "0".

提交回复
热议问题