Home > Software design >  Powershell script to run NPM install in multiple directories keeps launching multiple installs
Powershell script to run NPM install in multiple directories keeps launching multiple installs

Time:11-14

Hoping someone can assist me.

I am currently working on creating a project template for myself to use going forward for Electron/React.

Have most of it working except for my preinstall script.

File Hierarchy:
-/root
--/package.json
--/react
---/package.json
--/electron
---/package.json

Description: Have created a Powershell script Set-Path into each of these directories and run npm install for each of the 3 packages.

Issue: Script gets stuck in a loop installing over and over.

Code Example:

$root = Get-Location
$locations = $root,"$root/electron","$root/react"

Foreach ($location in $locations) {
    Set-Location $locations
    npm install
}

Set-Location $root

Does anyone know how to prevent this? Either by making Powershell wait for NPM install to run in each loop or by a better way to run npm install on 3 packages at once.

CodePudding user response:

Found the issue and want to post it incase anyone finds themselves in a similar bind.

Essentially the issue was with the NPM Package and how I set it up.

I had named the NPM script that calls this PS Script "Preinstall", and since this PS Script also would perform an NPM install for the package, this would result in an infinite loop.

Solutions:

  1. Remove initial NPM install from PS Script and let NPM Install via the CLI call the preinstall script as intended.
  2. Rename NPM Script to something else, however this would not be the solution that results in the most ease of use.

E.g. of NPM Script

"preinstall": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./preinstall.ps1"
  • Related