Why doesn't my 'npm install --global yarn' work? - node.js

After running npm install --global yarn, I get an output that says:
> yarn#1.22.18 preinstall C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)
C:\Users\(me)\AppData\Roaming\npm\yarn -> C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
C:\Users\(me)\AppData\Roaming\npm\yarnpkg -> C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
+ yarn#1.22.18
updated 1 package in 0.389s
When I run "yarn serve" it tells me that 'yarn' is not recognized as a command. I've tried this in powershell and CMD and neither works.

It ended up being a path issue. I used npm list -g to reveal where yarn was installed. Then I searched for "Edit the environment variables" in Windows and added the containing folder to the PATH.
I also updated my node and npm to the latest versions before doing these steps.

Just put --> npm install -g yarn
The problem is that you're installing nvm as root, and when npm runs the lifecycle scripts it downgrades the permissions, making it impossible to even run Node scripts, since the node binary can only be accessed by root.

Related

'yarn' is not recognized as an internal or external command, operable program or batch file

I installed yarn using the following command
npm install yarn -g
Then it shows the following status:
> yarn#1.22.10 preinstall C:\Users\vdine\AppData\Roaming\npm\node_modules\yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)
C:\Users\vdine\AppData\Roaming\npm\yarn -> C:\Users\vdine\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
C:\Users\vdine\AppData\Roaming\npm\yarnpkg -> C:\Users\vdine\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
+ yarn#1.22.10
added 1 package in 0.962s
Then I added the yarn path to the environment variable
C:\Users\**path**\AppData\Roaming\npm\node_modules\yarn\bin
but its still shows "'yarn' is not recognized as an internal or external command,
operable program or batch file."
What should I do now?
Updated Jan '21
The recommended method for installation Yarn is now via npm:
npm install --global yarn
https://classic.yarnpkg.com/en/docs/install/
Try adding C:\Users\vdine\AppData\Roaming\npm to your PATH environment variable instead of C:\Users\**path**\AppData\Roaming\npm\node_modules\yarn\bin
Previously
The recommended method for installing Yarn is your OS tools instead of npm, check out the MSI method:
http://web.archive.org/web/20201226122851if_/https://classic.yarnpkg.com/en/docs/install/#windows-stable
remove file C:\Users\vdine\AppData\Roaming\npm\node_modules\yarn
and run npm install -g yarn --force

npm command 'serve ' not found, although it is installed

I have installed serve with npm as "npm install serve -g" and also with yarn "yarn global add serve", but when I try to run "serve -s build" it says that "Command 'serve' not found.
You should not install the packages globally.Try to do the following-
npm uninstall -g serve
npm i -S serve
Let me know if this works.
I had same problem too and this helped me to fix it so try this after installing serve;
npx serve -s build
or
npx serve -s build -p 8000
(8000 = it depends by your choice)
I don't know why but this worked for me
None of these above answers worked for me, so this is what works for me :
sudo su
npm install -g serve
Installing as root helps globally installing serve
Make sure to have this in your .bashrc or .zshrc
if you're using Yarn:
export PATH="$PATH:$(yarn global bin)"
if you're using NPM:
export PATH="$(npm bin -g):$PATH"
So that the shell would know where to look for executables such as serve, npx, live-server etc that are installed globally.
Make sure to reload your shell config:
source ~/.bashrc // or ~/.zshrc
If anyone still gets the problem, try this:
npm uninstall -g serve
npm i -S serve
yarn global add serve
I faced the same problem, what I did was run the command yarn serve -s build
If you got it installed with npm then you can just add npm before the suggested command

NPM Not functioning correctly

My NPM has been acting up recently. I have been having issues installing packages globally. For example when I type npm install -g ionic, ionic will work where I ran that, but nowhere else on my computer. Also when I type npm install, it will install all sorts of files directly to the folder instead of to the node_modules folder.
Taken from: How to restore/reset npm configuration to default values?
To reset user defaults
Run this in the command line (or git bash on windows):
echo "" > $(npm config get userconfig)
npm config edit
To reset global defualts
echo "" > $(npm config get globalconfig)
npm config --global edit
If you need sudo that run this instead:
sudo sh -c 'echo "" > $(npm config get globalconfig)'

Jenkins is not installing some node dependencies

I'm running a nodejs application's build on jenkins. I run node as shell script step, because I have some limitations in terms of the plugins I can install in this jenkins instance.
This is what the npm install step looks like:
#!/bin/bash +x
source ~/.bashrc
cd $WORKSPACE/ && \
nvm use 7.8.0 && node --version && npm install
The problem I have is, when npm install finishes, it doesn't install everything. If I ssh into the box where jenkins is installed and run npm install inside that project's workspace, with the same user jenkins uses, it works ok. Any ideas?
EDIT
I just realized the dependencies that it's not installing are devDependencies
The problem was I had the node env set to production, and of course, it wouldn't get the devDependencies...

npm installing dev dependencies on production

I set NODE_ENV to production and tried to install dependencies using a Capfile which contains this:
run "cd #{latest_release} && npm config set production=true && npm install --production"
or this:
run "cd #{latest_release} && npm install --production"
but I always get also the dev dependencies, which is annoying because after a few releases all the inodes are taken and I cannot create any other files on the deploy machine.
I set the environment variable like this in the Capfile:
set :default_environment, {
'NODE_ENV' => 'production'
}
run "echo $NODE_ENV"
and it echoes the correct value.
If I execute
npm install --production
from within a shell, it works correctly. The user that makes the capistrano deploy and this shell user are the same, so I'm quite lost. Any hints?
The problem was:
sudo npm link
which I ran after the install command and which installs all dependencies. The fix is:
sudo npm link --production

Resources