0

I'm trying to run npm i in another directory from my node.js application. The reason for this is because there is a child process that is spawned in that directory. The parent app is on user's systems, so they cannot be expected to run npm i when new updates are pushed. I can copy the package.json to each directory where child processes could run, so that running install manually there would install the needed dependencies.

Austin
  • 347
  • 7
  • 21
  • Does this answer your question? [npm - install dependencies for a package in a different folder?](https://stackoverflow.com/questions/13498403/npm-install-dependencies-for-a-package-in-a-different-folder) – Marwi Oct 29 '20 at 20:23
  • @MarwanAmireh that is for installing via command line. I need to be able to do it programmatically through code. – Austin Oct 29 '20 at 20:24

2 Answers2

0

Are you attempting something like this?

var cwd = __dirname;

var Proc = require ("child_process").exec (
    "npm i", 
    {
        shell: "/bin/sh",
        stdio: [ 'pipe', 'pipe', 'pipe'],
        cwd
    }
);

Proc.stdout.on ("data", function (data) {
    process.stdout.write (data.toString ());
});

Proc.stderr.on ('data', function (data) {
    process.stderr.write (data.toString ());
});

Proc.on ('close', (code) => {
    console.log (`close: ${code}`);
});
Bryan Grace
  • 1,826
  • 2
  • 16
  • 29
0

Here's what ended up working:

var Proc = child_process.exec ('npm i', {
            cwd: '/path/to/folder/to/run/in'
          }, function(error, stdout, stderr) {

          });
Austin
  • 347
  • 7
  • 21