How to Check the Current Node.js Version at Runtime
To check the current Node.js version at runtime, you have a few options.
The first option is to use the version
property of the process
object. By running process.version
, you will receive a string that represents the current Node.js version. Here is an example:
1 | console.log(process.version); |
However, please note that this approach will result in a ReferenceError in the browser, as the process
object is not defined there.
Alternatively, you can use process.versions
(with an ‘s’) to access an object containing properties that reference the versions of various Node.js components. Here is an example:
1 | console.log(process.versions); |
To extract the major version number (e.g., 12
), you can use the following code:
1 | console.log(process.versions.node.split('.')[0]); |
By splitting the version string on the period (.), you can access the major version number.
This way, you can easily check the current Node.js version at runtime.
tags: [“Node.js”, “runtime”, “check”, “version”]