6 examples of 'node version check' in JavaScript

Every line of 'node version check' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16function checkNodeVersion() {
17 if (!semver.satisfies(process.version, '>=2.0.0')) {
18 var engine = semver.lt(process.version, '1.0.0') ? 'Node' : 'io.js';
19 var message = 'You are currently running ' + engine + ' ' +
20 process.version + '.\n' +
21 '\n' +
22 'React Native is moving to io.js 2.x. There are several ways to upgrade' +
23 'to io.js depending on your preference.\n' +
24 '\n' +
25 'nvm: nvm install iojs && nvm alias default iojs\n' +
26 'Homebrew: brew unlink node; brew install iojs && brew ln iojs --force\n' +
27 'Installer: download the Mac .pkg from https://iojs.org/\n' +
28 '\n' +
29 'About io.js: https://iojs.org\n' +
30 'Follow along at: https://github.com/facebook/react-native/issues/1737';
31 console.log(formatBanner(message, {
32 chalkFunction: chalk.green,
33 marginLeft: 1,
34 marginRight: 1,
35 paddingBottom: 1,
36 }));
37 }
38}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

116async function checkNodeVersion(minimalNodeVersion) {
117 const spinner = ora('Checking node version').start()
118
119 let nodeVersion
120 try {
121 const { stdout } = await exec('node --version')
122 nodeVersion = stdout.trim()
123 } catch (err) {
124 spinner.fail(`node version check failed\n${err}`)
125 throw new Error(err)
126 }
127
128 if (compareVersions(nodeVersion, minimalNodeVersion) === -1) {
129 spinner.fail(
130 `You need Node.js v${minimalNodeVersion} or above but you have v${nodeVersion}`
131 )
132 throw new Error()
133 }
134
135 spinner.succeed(`Node version ${nodeVersion} ${printOk()}`)
136}
3module.exports = function checkNodeVersion(nodeVersion) {
4 var returnOutput = {
5 warn: false,
6 warning: warning
7 };
8 if (nodeVersion) {
9 nodeVersion = nodeVersion.split('.')[0];
10 if (nodeVersion < 1) {
11 returnOutput.warn = true;
12 }
13 }
14 return returnOutput;
15};
178function validateNodeVersion(version) {
179 const [major, minor] = parseSemver(version);
180
181 if (major < 7 || (major === 7 && minor < 6)) {
182 throw new Error(
183 'Running the end-to-end tests requires Node version 7.6.0 or higher.'
184 );
185 }
186}
87static getNodeVersion(): string {
88 return process.versions.node;
89}
42check(): Promise<NodeInfo> {
43 return new Promise((resolve, reject) => {
44 let batch = new DefaultBatch();
45 let nodeInfo: NodeInfo = {
46 chain: 'unknown',
47 chainId: 0,
48 clientVersion: null
49 };
50 batch.addCall('eth_getBlockByNumber', ['0x00', false])
51 .then((block) => {
52 if (block.hash === NodeChecker.ETC_MAINNET_GENESIS) {
53 nodeInfo.chain = nodeInfo.chain || 'ethereum';
54 nodeInfo.chainId = nodeInfo.chainId || 1;
55 } else if (block.hash === NodeChecker.ETC_MORDEN_GENESIS) {
56 nodeInfo.chain = 'morden';
57 nodeInfo.chainId = 62;
58 }
59 }).catch(reject);
60 batch.addCall('eth_getBlockByNumber', ['0x1d4c00', false])
61 .then((block) => {
62 if (block.hash === NodeChecker.ETC_BLOCK_1920000) {
63 nodeInfo.chain = "etc";
64 nodeInfo.chainId = 61;
65 } else if (block.hash === NodeChecker.ETH_BLOCK_1920000) {
66 nodeInfo.chain = "ethereum";
67 nodeInfo.chainId = 1;
68 }
69 }).catch(reject);
70 batch.addCall('web3_clientVersion', [])
71 .then((version) => {
72 nodeInfo.clientVersion = version;
73 })
74 .catch(reject);
75 this.ethRpc.rpc.execute(batch)
76 .then((_) => resolve(nodeInfo))
77 .catch(reject);
78 });
79}

Related snippets