Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Node.js with Windows PowerShell PackageManager (OneGet)?

I tried Install-Package nodejs, which seems to have done something:

PS C:\WINDOWS\system32> Install-Package nodejs

The package(s) come(s) from a package source that is not marked as trusted.
Are you sure you want to install software from 'chocolatey'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y

Name                           Version          Source           Summary
----                           -------          ------           -------
nodejs.install                 11.10.0          chocolatey       Node JS - Evented I/O for v8 JavaScript.
nodejs                         11.10.0          chocolatey       Node JS - Evented I/O for v8 JavaScript.

Then when I try to run the node command, it fails:

PS C:\WINDOWS\system32> node
node : The term 'node' 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.
At line:1 char:1
+ node
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (node:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

How do I do it properly with PackageManager, so that the command is then available?

Not sure if this other question is related: Chocolaty packages not installing via OneGet/PackageManagement in Windows 10?

As that other question suggests, I tried setting Set-ExecutionPolicy Unrestricted, the uninstalling and reinstalling nodejs, but the result is the same.

I do see that after install the following files exist:

C:\Chocolatey\lib\nodejs.11.10.0\nodejs.11.10.0.nupkg
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg\legal\LICENSE.txt
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg\legal\VERIFICATION.txt
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg\tools\chocolateyInstall.ps1
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg\tools\node-v11.10.0-x64.msi
C:\Chocolatey\lib\nodejs.install.11.10.0.nupkg\nodejs.install.11.10.0.nupkg\tools\node-v11.10.0-x86.msi

which makes me think that maybe it expects me to run the installer manually after?

My goal is to simply install anything I need on a new Windows machine from a script, without having to download/install them all manually with GUIs.

like image 769
trusktr Avatar asked Feb 18 '19 02:02

trusktr


1 Answers

I was able to reproduce this, but also was able to resolve it. The issue is that the node executable isn't on the PATH after installing with Install-Package. However, this does make some sense as the current PowerShell process won't pick up outside changes to environment variables until it is relaunched.

Chocolatey's refreshenv command works sometimes but not always, and after installing nodejs with Install-Package, refreshenv did not work to refresh the PATH variable. What did work was launching a new PowerShell session and running the node command, which ran the executable successfully:

> node

Welcome to Node.js v16.7.0.
Type ".help" for more information.
> 

choco.exe can sometimes mask this but usually Chocolatey won't generate shims for executables placed by EXE or MSI installers (unless the package maintainer forcibly creates them).

Tip: In case you are in a situation where you need to call the binary from the same process that installed the package (e.g. during a chef-client run where refreshenv isn't an option), use the fully-qualified path to the executable to invoke the newly-installed software.


Additionally, don't use the Chocolatey OneGet provider in real automation; it's not yet stable and looks like it hasn't seen any development activity in five years at this time of writing. From the README.md:

NOTE: Seeking maintainers to help finish this Provider. Please inquire on the issues list or on Gitter (see the chat room below). Thanks!

There is an alternative listed in the same README:

NOTE: For now, you may be more interested in using the ChocolateyGet provider as a stop gap solution until this provider is ready. See https://github.com/jianyunt/ChocolateyGet for details

I tried this and it seems to work, but I would still recommend using the official choco.exe binary. It's easy to install and you'll get the best support when using it. It's also telling that configuration management solutions (such as Chef's chocolatey_package resource) continue to make use of the binaries and will generally list installing the client as a prerequisite, even though technically the PackageManagement module could be used as a native solution instead.

like image 145
Bender the Greatest Avatar answered Nov 15 '22 00:11

Bender the Greatest