NodeJS spawn a process to run a shell command

JS
S
JavaScript

Spawns a shell then executes the command within that shell, buffering any generated output. Note: will block the Node.js event loop! To run: node runner.js https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

1// npm-version-checker.js
2'use strict'
3const { execSync } = require('child_process');
4
5function exec (cmd) {
6  return execSync(cmd).toString().trim()
7}
8
9module.exports = function () {
10  const version = exec('npm --version');
11  console.log(version);
12}
13
14//runner.js
15require('./check-versions')()

Created on 1/31/2018