Yarn Install - Not recognized as command in Windows 11 Powershell - powershell

Yarn's not recognized as a cmd command.
I tried installing it globally with 'npm install -g yarn' and adding a path variable to "C:\Users[user]\AppData\Roaming\npm\yarn".
Any other suggestions?

First thing first, you should check you have 16+ node.js installed and, you can check it with
node -v
If it's valid,
run the npm config get prefix it'll give you a path CTRL+C this path
Open the start search and type in env and then click "Edit the system environment variables"
Modify the Environment variables
Add the copied path to you PATH as new
After this you should be good, but if the PS is cannot load the Yarn run this command
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Here a few things you can try:
You may need to close and reopen the Powershell windows because newly added environment variables or updates to them are not automatically reflected to already open shell windows.
Check if your path is correct: it normally is C:\Users\endo\AppData\Roaming\npm on my PC, not C:\Users\endo\AppData\Roaming\npm\yarn as you mentioned.
Try running yarn.cmd or yarn.ps1.
Try CMD instead of Powershell, see if it works that way.

Related

How to activate conda environment from powershell?

Version Anaconda 4.8.3
What I cannot do:
I can't activate any environment from powershell.
conda activate base
What I can do:
conda env list
conda create -n xxx conda remove -n xxx
I have tried many solutions, but they turn out to be useless:
conda init powershell
restart powershell
conda activate
conda update -n base conda and redo 1
conda install -n root -c pscondaenvs pscondaenvs
Set-ExecutionPolicy RemoteSigned
Run activate base
The result of running conda activate
You don't need Admin permission.
Once you install Anaconda or Miniconda on Windows, open a Anaconda Powershell Prompt from Start Menu.
Or, If you don't see it there, then assuming you have installed
miniconda3 at path C:\miniconda3\4.9.2, do:
powershell -ExecutionPolicy ByPass -NoExit -Command "& 'C:\miniconda3\4.9.2\shell\condabin\conda-hook.ps1' ; conda activate 'C:\miniconda3\4.9.2' "
Now try:
conda init powershell
and reopen powershell.
Additional note: By default conda will autoactivate itself, when we open terminal. If you prefer not, then disable auto-activation with:
conda config --set auto_activate_base false
Open a Anaconda Powershell Prompt from Start Menu.
Now Try:
conda init powershell
Now restart the powershell, if find some error like this in powershell:
\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3
Then change the execution policy. Type this code to powershell:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
To find more with execution policy you can visit this link.
Here is my easier solution which works with Anaconda, Miniconda, and even Miniforge:
Open Powershell and browse to condabin folder in your conda installation directory, for example: C:\Users\<username>\anaconda3\condabin
Run ./conda init powershell in that folder, and re-open the powershell.
Please note: If you encountered ps1 cannot be loaded because running scripts is disabled on this system, simply run the Powershell as Administrator and enter the following: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Restart the Powershell & Enjoy!
When you use Anaconda or Miniconda type in Anacondaprompt:
conda init powershell
In order to activate conda on Powershell, I just executed this command:
C:\Users\<username>\anaconda3\shell\condabin\conda-hook.ps1
Then I could see all environments without needing to restart Powershell.
conda env list
conda activate base
So you need to basically just run the conda-hook.ps1 script.
Encountered the same problem. The trick is to make sure that the environment is included in the PATH variable of Windows Powershell. To view the PATH variables enter the following in the powershell:
$env:Path.split(';')
If the environment you desire doesn't show up in the output list, as in
.....
.....
..../envs/yourEnv/...
.....
run the following command on cmd:
conda init powershell
then restart _Anaconda Powershell,
then:
conda activate yourEnv
Hopefully, now the conda prompt should now show your desired environment.
To solve the problem without changing te security policy on powershell, you need to be on powershell 7. Powershell 7 will let you use a \WindowsPowerShell\profile.ps1 file as startup script with default (restricted) security policy settings. Windows Powershell is by default (on windows 10 at 14-08-2022) version 5.x.
Check your powershell version with
$PSVersionTable
If you're still on powershell 5, install powershell from the windows store. This will install powershell 7 alongside powershell 5. It will probably be called Powershell instead of Windows Powershell. Or if you're not allowed to use the windows store, ask IT to install it for you. Check the version again in the new powershell.
After this, we can follow the answer of prashant:
Open Anaconda prompt and run
conda init powershell
To stop conda starting by default run
conda config --set auto_activate_base false
& 'C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1'; conda activate 'C:\ProgramData\Miniconda3'
This is the command found in Anaconda Powershell Prompt (Miniconda3).lnk on my computer.
On my machine, I can enter the environment anywhere by executing these two commands.

WSL Interop PATH customization

I have an Ubuntu-18.04 WSL instance installed and have a large set of scripts installed in it at /home/username/bin/scripts.
I have also updated the $HOME/.bashrc, $HOME/.bash_profile, /etc/bash.bashrc, /etc/profile and /etc/environment files to update $PATH to include /home/username/bin/scripts in the path.
*I'm well aware i only usually need it in my $HOME/.bash_profile, lots of places from experimenting
Say I have a script called sampleScript in /home/username/bin/scripts, when i try to run the command:
wsl sampleScript
It gives the error: /bin/bash: sampleScript: command not found
If I open up wsl in interactive mode I can run it just fine, but I would like to be able to expose the script to not be run in interactive mode.
I've tried making an environment variable SPATH='/home/username/bin/scripts' and setting WSLENV=SPATH/p, but it also does not show up if i do wsl echo $PATH.
*I'm doing this all in command line instead of powershell b/c powershell doesn't appear to be able to do wsl echo $PATH at all, it just returns empty
I have also tried: wsl -u username sampleScript; with no luck
Is there a way to customize the PATH for wsl interop mode or am i doing something incorrectly with WSLENV?

How to start WSL terminal in a current powershell directory?

I can open a powershell and type
> Ubuntu
to open a WSL ubuntu shell on windows 10. This will always take me to the WSL home directory. How would I instead open the terminal in the same location that the powershell is currently in?
FYI. I need this for creating a right click "open terminal here" type registery key for windows explorer.
If we look at what running ubuntu.exe actually does:
PS C:\> ubuntu.exe /?
Launches or configures a Linux distribution.
Usage:
<no args>
Launches the user's default shell in the user's home directory.
install [--root]
Install the distribution and do not launch the shell when complete.
--root
Do not create a user account and leave the default user set to root.
run <command line>
Run the provided command line in the current working directory. If no
command line is provided, the default shell is launched.
config [setting [value]]
Configure settings for this distribution.
Settings:
--default-user <username>
Sets the default user to <username>. This must be an existing user.
help
Print usage information.
We can see that it by default launches the WSL shell inside the home directory. If we want run inside it inside the current directory open in PowerShell, we need to specify the run option. So the full command will be ubuntu.exe run.
Another option is to just run wsl.exe or bash.exe. These commands will by default open WSL in the current working directory.
Note: We don't need to specify the .exe after the commands. Running ubuntu, wsl and bash all work as well. PowerShell knows how to run executables without specifying the extension.
method 1:
set Ubuntu to wsl default distribution by wsl --set-default Ubuntu
just type wsl and you are in the current Powershell directory
method 2:
open other folder
wsl.exe --cd $pwd or wsl.exe --cd "path"

Ember.js fails to install globally

I ran the command to install Ember.js:
npm install -g ember-cli
Then when I run:
Ember -v
I get error: "The term ember is not recognized as the name of a cmdlet, function, script file or operable program ..."
I added the system environment variable $NODE_PATH = %AppData%\npm\node_modules
I see ember-cli folder in the $NODE_PATH
This is a newly imaged machine so this may be an issue with my npm setup/configuration. How can I install ember globally?
I added %AppData%\npm (use the full path which in my case is C:\Users\bmackey\AppData\Roaming\npm) to the system Path environment variable. I had to remove C:\Program Files\Microsoft DNX\Dnvm\ from my Path in order to stay under the 260 character limit; hopefully I won't need this. I do not know a way around the 260 character limit.
Now ember -v works.
You need to add the path to the ember.cmd file into the powershell environment variable, note that this is different to the standard PATH environment variable.
For those that don't know, the reason you can run ember (a node module) simply by running the command ember is because when you install ember it create a ember.cmd file in your AppData folder:
this file typically looks like this and just bootstraps node and runs the ember js file:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\node_modules\ember-cli\bin\ember" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\node_modules\ember-cli\bin\ember" %*
)
so when you run ember from your command window or powershell it just looks for a cmd file in its PATH variable. If this doesn't have an entry pointing at the location of this cmd file it won't be able to run it.
To fix this in powershell just run the following:
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Users\<user>\AppData\npm", [EnvironmentVariableTarget]::Machine)
Most of this is taken from the answer here.
Make sure C:\Users\<user>\AppData\npm is where NPM has deployed your ember.cmd file. I have seen this also deploy in C:\Users\<user>\AppData\Roaming\npm so it can vary.

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"