1

How to run npm pack command from my node application?

something like this:

const npm = require('npm')
const result = npm.pack('sourcefolder', 'targetdir');

//result ==== down..
Jon Sud
  • 10,211
  • 17
  • 76
  • 174
  • Possible duplicate of [Can I install a NPM package from javascript running in Node.js?](https://stackoverflow.com/questions/15957529/can-i-install-a-npm-package-from-javascript-running-in-node-js) – jonrsharpe Jan 19 '19 at 08:38
  • no, var npm = require('npm'); is not longer exist.. – Jon Sud Jan 19 '19 at 08:50

2 Answers2

1

Everything that can be ran from command line can be ran from JS, so yes:

require('child_process').exec('npm pack', console.log)
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
0

You don't have to run npm pack command to create tarball.

Just use tar package:

const tar = require('tar');
const fstream = require('fstream');

function pack({ path, target }) {
  fstream
    .Reader({ path, type: 'Directory' })
    .pipe(tar.Pack({ noProprietary: true }))
    .pipe(fs.createWriteStream(target));
}

const name = 'your-folder-package';

pack({
  path: path.join(__dirname, 'packages', name),
  target: path.join(__dirname, 'dist/packages', `${name}.tar`),
});
Shlomi Levi
  • 3,114
  • 1
  • 23
  • 35