Skip to content

Instantly share code, notes, and snippets.

Last active August 22, 2022 13:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Node script to run NPM commands programatically. This example runs `npm list` and parses the output to get the currently installed versions of project dependencies.
const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const package = require('../package.json');
const dependencies = Object.keys(packageData.dependencies).map((dep) => dep);
let promises = [];
if (dependencies && dependencies.length) {
const filteredList = ['@wtf-ds/core', 'react', 'styled-components'];
promises = (name) => {
const { stdout } = await exec(`npm list --depth=0 ${name} | grep ${name}`);
const idx = stdout.indexOf(name);
const version = stdout.substring(idx + name.length + 1).replace('\n', '');
return { name, version };
Promise.all(promises).then((result) => {
const data = JSON.stringify(result, null, 2);
fs.writeFileSync('dependencies.json', data);
Copy link

Check out my post here for more details on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment