prerak6962
Posts: 112
Joined: Mon Dec 08, 2014 5:26 pm

Powershell npm install

I have an installer containing a powershell script which installs a npm module and does some actions after that using that module.
If I execute that script from powetshell console, it works completely fine. However, when the installer executes it, the module gets installed but for the next commands, it fails.

This is what my script does:

Code: Select all

npm install pm2 -g
pm2 describe TestApp
This works fine from powershell console, but in the installer, I get this error:

Code: Select all

Caught Exception: The term 'pm2' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
The script is executed under following environment:
  • Execution time: When the system is being modified (deferred)
    Execution Options: Run under the LocalSystem account with full priviliges
    Advanced Installer version: 13.5
    Powershell version: 5.1.14409.1012
    Node version: 8.11.2
    NPM version: 5.6.0
Is there anything that I am missing to make it work correctly?

I have attached the .aip file which reproduces this issue.
Attachments
PowershellTest.aip
(13.46 KiB) Downloaded 370 times
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell npm install

Hello,

This could happen because the PowerShell custom action runs under LocalSystem account context. And the path to theNPM tool is not added under Path environment variable of LocalSystem account.

Can you disable the "Run under the LocalSystem account with full privileges" option for your custom action, rebuild and test the setup again? And see if this helped.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
prerak6962
Posts: 112
Joined: Mon Dec 08, 2014 5:26 pm

Re: Powershell npm install

Hi Daniel,

I tried by disabling the "Run under the LocalSystem account with full privileges" option, but the output is still the same. it terminates with the same error.

Prerak
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell npm install

Hello Prerak,

Try unchecking the "64-bit script" option too for your custom action and see if this matters. Otherwise please try to reference by using a full path the NPM tool in your script code.

Hope this helps.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
prerak6962
Posts: 112
Joined: Mon Dec 08, 2014 5:26 pm

Re: Powershell npm install

Hi Daniel,

Is there a way to access variables (AppDataFolder in my case) in the powershell script file (not inline script)?
I can do it in an inline powershell script but, in case of failure, it does not show the error message.

I tried something like this:

Code: Select all

[#DeployServer.ps1] --location [AppDataFolder]
in the "File Path" for "Run PowerShell script file" but it says file not found.

I need this to construct the full path of the npm modules.

Prerak
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell npm install

Hello Prerak,

Unfortunately when using a "Run PowerShell Script file" action type, the only way to access installer property is by using this syntax:

Code: Select all

$propValue = AI_GetMsiProperty YOUR_PROP
Also, this will work only for actions executed during the immediate execution time. Within a deferred executed "Run PowerShell Script file" action type you cannot access the installer properties. In this case, as a workaround you can try to access the %appdata% environment variable path. Have a look on "Getting the path of %AppData% in PowerShell" thread.

All the best,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
prerak6962
Posts: 112
Joined: Mon Dec 08, 2014 5:26 pm

Re: Powershell npm install

Hi Daniel,

Thank you for the details.

I was able to access the npm package in "Inline powershell script" by adding the installation folder to the "Path" environment variable.

Code: Select all

$appData = AI_GetMsiProperty AppDataFolder
$env:Path += -join(";", "$appData", "npm")
I have to do this in each of the powershell script in which I want to access the npm package.

Prerak
Daniel
Posts: 8238
Joined: Mon Apr 02, 2012 1:11 pm
Contact: Website

Re: Powershell npm install

You are always welcome Prerak.

Thank you for your follow up on this. I'm sure your posted solution will be useful for future users facing the same scenario.

Have a nice day,
Daniel
Daniel Radu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”