yarn command not found after installing via npm - javascript

As per the yarn installation for yarn v2, they want you to install using npm install -g yarn. So I ran sudo npm install -g yarn on Ubuntu 20.04. But after I do that, it says command not found.
❯ sudo npm install -g yarn
> yarn#1.22.10 preinstall /usr/local/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)
❯ yarn --version
zsh: command not found: yarn

sudo npm install -g npm
then
sudo npm install -g yarn
Then reboot your system. That did it for me.
Before a reboot only sudo yarn worked. I tried looking at file permissions but everything seemed in order and the files were executable as expected. Nevertheless after a reboot it worked.
If you go to /usr/local/bin after the installation there's a link there to where yarn.js lives, as expected, and file permissions for it were also correct.
/usr/local/bin is added to $PATH, so it's surprising that it doesn't see the new cmd right away, but perhaps it didn't reload or map it until after the reboot? I don't know. But I just spent a good hour trying to figure this out so I'm posting what worked for me to spare other the hassle.

TL;DR
If you are managing node via nvm, then probably the path to yarn binary is not included in the $PATH variable. You should add this -
# Add this at the end (or after the $NVM_DIR initialization)
# in your profile - .bashrc | .zshrc | .profile, etc
export PATH="`yarn global bin`:$PATH"
at the end of your profile file (.zshrc for me) or at least after the $NVM_DIR initialization.
I have recently faced this issue and while searching for a solution, I landed up here.
Here is what my environment looks like:
OS: Ubuntu 20.04
Shell: zsh
NodeJS: managing it via nvm, and NOT apt.
After going through all the answers, I was not keen on uninstalling anything. So I tried to dig a bit deeper.
I installed yarn via npm install -g yarn command. So the first thing I wanted to verify was the location of the yarn binary. To do this, I ran the command where yarn which lists the installation path for the yarn binary.
$ where yarn
/home/<user_name>/.nvm/versions/node/v16.11.1/bin/yarn
Then it hit me. In my .zshrc file, I had added the yarn global bin command (which spills out the directory of all the global packages installed by yarn) at the top like so:
# Top of my .zshrc file
export PATH="`yarn global bin`:$HOME/bin:/usr/local/bin:$PATH"
and as per the installation instruction of nvm, the $NVM_DIR (the variable which holds the nvm directory path) was added at the end of my .zshrc file.
So when I was starting up my shell, it was actually trying to load the yarn command (present inside the nvm directory) even before loading the $NVM_DIR path.
To solve this, I tweaked my .zshrc file and moved the yarn global bin command after the $NVM_DIR like this:
# Top of my .zshrc file
export PATH="$HOME/bin:/usr/local/bin:$PATH"
# ...
#
# Something in between
#
# ...
# Bottom of my .zshrc file
export NVM_DIR="${HOME}/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# Here is where I have added the path to yarn global
export PATH="`yarn global bin`:$$PATH"
I hope that this would be of help.

This solved it for me:
corepack enable
(if you get "Internal Error: EACCES: permission denied", run it with sudo)
This is also recommended by the Yarn documentation: https://yarnpkg.com/getting-started/install

Uninstall cmdtest:
sudo apt remove cmdtest
Then, run these commands:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

If you want to avoid reboot, use
/usr/local/lib/node_modules/yarn/bin/yarn --version

The yarn documentation is missing a step, you need to restart your computer between this installation and running yarn --version.
This worked for me

I recently had a similar situation and here is how I solved it.
First I troubleshoot the current npm installation:
npm config -list
I had a ~/.npmrc file that had a different prefix:
PREFIX=/opt/homebrew
That made my npm installation look for globally installed packages under /opt/homebrew.
In my case, I'm using a different npm installation (not with homebrew anymore). A simple fix is to remove this custom PREFIX from the ~/.npmrc file and the problem was solved.
Now npm looks for globally installed packages under /usr/local/bin/.

I installed yarn with npm install -g yarn on git bash and I tested it with yarn -v that show the version of the installed yarn, but when I used yarn start it gives me this error
C:\Users\{username}\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found
These are simple steps that I used to fix my problem on Windows 10:
Uninstall node.js
Restart your computer
Delete your C:\Program Files\nodejs and C:\Users\{username}\AppData\Roaming\npm
Install node.js again and check it with node -v
Start your vs code as an admin and write npm install
Write yarn start

Related

nest Command not found

I followed the documentation to create my first NestJS project.
Installing the Nest CLI with the command npm i -g #nestjs/cli was successful.
The output was:
+ #nestjs/cli#6.3.0
updated 1 package in 11.326s
However, when I try to scaffold a project with the Nest CLI:
nest new project-name
I get the following error:
zsh: command not found: nest
Some details about my environment:
OSX
iTerm
npm version 6.5.0
Use npx #nestjs/cli instead of nest
e.g. npx #nestjs/cli g controller pages
I was also getting the same console error when npm installing!
Erro was
#nestjs/cli
npm WARN deprecated resolve-url#0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix#0.1.0: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated chokidar#2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated fsevents#1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/#nestjs/cli
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/#nestjs
npm WARN notsup Unsupported engine for watchpack-chokidar2#2.0.0: wanted: {"node":"<8.10.0"} (current: {"node":"12.14.1","npm":"6.13.4"})
npm WARN notsup Not compatible with your version of node/npm: watchpack-chokidar2#2.0.0
How I wasn able to install =>
Solution
sudo npm install -g #nestjs/cli
Following command works in my windows 10
node -v
v14.16.0
npm -v
6.14.11
npx #nestjs/cli new project-name
I was having the same issue, what I did was:
Installed the latest node with npm install npm#latest -g
and then added the following to my .bash_profile
alias nest="/usr/local/Cellar/node/11.9.0/bin/nest"
Tested with nest --version and it worked.
For some reason the path is not being called correctly, hope it helps.
Working through a NestJS/Mongo DB tutorial i stumbled into this very same problem. After trying all of the solutions that were listed above, even turning my default profile back to bash instead of using zsh, i found that simply adding the /usr/local/bin and ~/.npm-global/bin to my path variable resolved the issue. Hopefully this helps someone else who may stumble on this in the future as well.
export $PATH="/usr/local/bin"
export $PATH="~/.npm-global/bin"
Were the two commands i ran from iTerm2 in order to add them to my PATH environment variable. Once this was done a simple
env
showed the entries in my $PATH environment variable and the nest -v command worked without any issues.
I had same error when I use GitBash.
I resoved it:
If you use windows:
Your use CMD: npm i -g #nestjs/cli
Then you can use gitbash:
Check version nestjs: nest --version
Create app nestjs: nest new name_project
I have faced same problem and solved ,following this
write the command NPM root -g
check where NPM is installed
add it to your path environment variable
You have to add the following line to your ~/.zshrc file:
source $HOME/.bashrc
This is needed so that the npm binaries are available on startup.
After all the answers that I tried, I used my method
After installing npm i -g #nestjs/cli in my mac terminal.
open bash file in macos by typing open ~/.bash_profile in mac terminal. Like you can type it anywhere.
Your bash will look like this and use this line
alias nest="~/.npm-global/lib/node_modules/#nestjs/cli/bin/nest.js"
Restart your mac for changes to be reflected in your terminal.
There you go nestjs cli is now working globally 2nd screenshot.
I have just succeeded with
npx #nets/cli new project
when asked what manager to choose from npm / yarn / pnpm
made choice for pnpm
cd project
npm run start
Take care!
Must use sudo in comandline for linux, otherwise the command nest cannot be add to /usr/local/bin folder:
sudo npm install -g #nestjs/cli
You can try to use sudo
sudo yarn global add #nestjs/cli
I got the same issue.
I just uninstalled and reinstalled nextjs.
Finally, I can use next -v

GitLab CI with JS Linting

I have 0 experience with GitLab Continuous Integration and I need to setup a job to run ESLint on .js files.
I've already read the GitLab CI and Pipeline documentations, along with some Git Hooks, but I still have no idea how to setup this, so any detailed and starting from the very beginning answer is appreciated.
First you need to setup your CI and have some runners available so they can run your continuous integration jobs. The easiest way for this is to use gitlab-ci-multi-runner (project is here along with documentation) along with the docker executor that will run your CI jobs in docker containers. Once you have configured some runners, add them to your Gitlab project so they're available to run jobs.
Once that's taken care of, you need to add a .gitlab-ci.yml file to your project. This file is used to describe the jobs that need to run during continuous integration etc. Here is an example (assuming you install eslint using npm)
image: node:latest
stages:
- lint
eslint:
stage: lint
script:
# Install ESLint in this docker container
- npm install -g eslint
# Configure ESLint (will read your .eslintrc file)
- eslint --init
# Run ESLint
- eslint <your_js_file>
Add your .gitlab-ci.yml file, commit and push the changes. The CI pipeline should start and run the above steps.
If you'd like to have comments to your PRs here is an example with eslint and pronto. (we have ruby app so we check ruby code style too )
image: 'circleci/ruby:2.5.1-node-browsers'
codestyle:
script:
- sudo apt -y install cmake
# install eslint dependencies
- sudo npm install -g eslint
- sudo npm install -g eslint-plugin-babel
- sudo npm install -g eslint-plugin-react
- sudo npm install -g eslint-plugin-import
- sudo npm install -g babel-eslint
- sudo npm install -g eslint-config-airbnb
- sudo npm install -g eslint-plugin-jsx-a11y
# install pronto runners
- gem install pronto --no-ri
- gem install pronto-rubocop --no-ri
- gem install rubocop-rspec --no-ri
- gem install pronto-eslint_npm --no-ri
# run linters
- vendor/ruby/bin/pronto run -f gitlab -c origin/dev --exit-code
You can also run linters separately:
- vendor/ruby/bin/pronto run -r eslint_npm -f gitlab -c origin/dev --exit-code
This piece -f gitlab -c origin/dev tells linters to check only changed lines of code.
Also if you use pronto-eslint_npm and want to check files in specific folder add
.pronto_eslint_npm.yml that will contain needed regex. (in my case it has next line)
files_to_lint: app\/frontend\/\S*(\.js|\.es6|\.jsx)$

Node js npm installed global package cannot be called from terminal, tried everything

I have installed some node js packages globally on my MAC.
I run $sudo npm install -g jshint in my terminal at my root directory which is /Users/apple/.
And I try to run $jshint myfile.js, but only got this error:
-bash: jshint: command not found
I have tried everything :
added NODE_PATH="/Users/apple/npm-global/lib/node_modules"
Changed $sudo chown apple /Users/apple/npm-global/
Changed $sudo chown apple /Users/apple
Changed $sudo chown apple /Users/apple/Tutorial/Nodejs/myfile.js
Any ideas? Thanks in advance!
EDIT
--------------------------------------
So I have reinstalled node js using brew, and I followed all the default folders which is usr/local/ , but still, $npm -g ls shows /usr/local/lib and my jshint package is successfully installed in /usr/local/lib, run $jshint, "-bash: jshint: command not found"...
run the following to see where global libraries are installed
npm list -g
Try not to use sudo with npm. If you can, uninstall npm and install it again with a simple brew install without sudo. Nevertheless, if you do not want to use Homebrew, npm has a guide for fixing issues like this.
npm config get prefix
will give you the npm's directory. If it's not /usr/local (the default on macOS), change it:
npm config set prefix /usr/local
Ultimately, if nothing works for you, you could try to chown npm's folder to your user.
Edit: If you want to keep the path /Users/apple/npm-global as your default npm path, you're going to have to add it to your $PATH global variable.
vi ~/.bash_profile
Append this command: export PATH=$PATH:/Users/apple/npm-global and then make sure to save the file.
Run this to apply the changes immediately: source ~/.bash_profile
Finally, verify that your path has been succesfully added: echo $PATH

browserify error /usr/bin/env: node: No such file or directory

I installed node js and npm via apt-get install and all of the dependencies, then I installed browserify
npm install browserify -g
it goes through the process and it seems like it installed correctly, but when I try to do a simple bundle per this walkthrough
I get the error:
/usr/bin/env: node: No such file or directory
Some linux distributions install nodejs not as "node" executable but as "nodejs".
In this case you have to manually link to "node" as many packages are programmed after the "node" binary. Something similar also occurs with "python2" not linked to "python".
In this case you can do an easy symlink. For linux distributions which install package binaries to /usr/bin you can do
ln -s /usr/bin/nodejs /usr/bin/node
New Answer:
Uninstall any nodejs package you've installed via your system package manager (dnf, apt-get, etc), delete any silly symlinks you've been recreating every upgrade (lol).
Install NVM,
use nvm to install nodejs: nvm install 6
Old Answer:
Any talk of creating symlinks or installing some other node-package are spurious and not sustainable.
The correct way to solve this is to :
simple install the nodejs package with apt-get like you already have
use update-alternatives to indicate your nodejs binary is responsible for #!/usr/bin/env node
Like so :
sudo apt-get install nodejs
sudo update-alternatives --install /usr/bin/node nodejs /usr/bin/nodejs 100
This now becomes sustainable throughout package upgrades, dist-upgrades and so forth.
Run apt-get install nodejs-legacy.
Certain linux distributions have changed node.js binary name making it uncompatible with a lot of node.js packages. Package nodejs-legacy provides a symlink to resolve this.
You can also install Nodejs using NVM or Nodejs Version Manager. There are a lot of benefits to using a version manager. One of them being you don't have to worry about this issue.
Instructions:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Once the prerequisite packages are installed, you can pull down the nvm installation script from the project's GitHub page. The version number may be different, but in general, you can download and install it with the following syntax:
curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh
This will download the script and run it. It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.
To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the ~/.profile file so that your current session knows about the changes:
source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
. . .
v0.11.10
v0.11.11
v0.11.12
v0.11.13
v0.11.14
As you can see, the newest version at the time of this writing is v0.11.14. You can install that by typing:
nvm install 0.11.14
Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 0.11.14
When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v
The comeplete tutorial can be found here
sudo apt-get install nodejs-legacy
This creates the symlink /usr/bin/node -> nodejs.
Source: https://lists.debian.org/debian-devel-announce/2012/07/msg00002.html
I seem the same problem when I build atom in Linux.
sudo apt-get install nodejs-dev
Fix my question.hope helpful to you.
If you don't want to symlink you could do this.
works in ubuntu
#!/usr/local/bin/node --harmony
harmony tag is for the new ECMAscript harmony
run the command which node the result will be something
/home/moh/.nvm/versions/node/v8.9.4/bin/node
Copy the path that you have got above then run the command in step 3.
ln -s /home/moh/.nvm/versions/node/v8.9.4/bin/node /usr/bin/node
You have to call "nodejs" and not "node". To verify this, type node -v on the shell: if nothing is found try nodejs -v. If that displays a version number, then the command you should be using is nodejs and not node. Therefore, you have to change the call to browserify in your script from node to nodejs (as shown below): replace
#!/usr/bin/env node
with
#!/usr/bin/env nodejs
You might also have to open the script as the superuser.

npm install vs sudo npm install -g

for some packages I have to run sudo npm install -g while for others npm install will suffice.
Why and what's the difference?
For example:
npm install -g grunt-cli # doesn't work
sudo npm install -g grunt-cli # works
npm install websocket-stream # works
Is sudo necessary only with the -g flag?
npm installs packages locally, ie. in a node_modules folder inside your current folder. This allows your application to depend on specific packages versions, without having to mess up with a global list of installed packages on your system. See the first paragraph of Isaac's blog post (Handle multiple versions of the same thing at the same time), which explains well how npm avoids the dependency hell often encountered in other programming ecosystems.
On the other hand, some packages are meant to be used as command line utilities, such as grunt-cli, mocha or json. In order to use them everywhere, you need to install them globally, hence the -g parameter.
Please note that you shouldn't need sudo to install global packages, see this relevant answer for more information.
Looks like permissions issue. -g install it globally (you will need to 'root'), but its not a good idea to install that as root
In terminal run:
sudo chown -R `whoami` ~/.npm
npm install -g grunt-cli installs the package in the global mode, every user could use it.
Without -g you just install it in the current directory.
If you are not the root user, you need to use sudo for the -g.
If you use npm without -g and you have write permission to the current directory, then
sudo is not necessary. Otherwise, you still need it.
-g is global, without just installs the package locally.
You run it with sudo as it installs to folders which your default user may not have access to by default.
grunt-cli will provide an executable that will be put in your PATH, so depending on how you configured your system it will require root access.
See this post from npm creator, particularly the part about using sudo with npm.
websocket-stream is a library, your code will use it so it will be easier to perform some tasks, usually it will be installed at the root of your project, in the node_modules folder.

Categories