npm install vs sudo npm install -g - javascript

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.

Related

yarn command not found after installing via npm

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

How to list available npm global packages

I have a few global packages available in my computer. I want to list them all using npm command without going to to the global installation folder found using npm root -g
npm list -g --depth=0
-g will look for the packages installed globally. If you need to check locally installed packages remove -g
--depth=0 will avoid every dependency in the tree

npm install -g is installing to a local folder

I have been using node and npm for a while, and I just started a larger scale project using it. Recently, however, whenever I run sudo npm install -g, it will install it into a ~/Programming/usr/local/bin instead of /usr/local/bin. Does anyone know why this is happening? How can I reset the installation location
You can set the location by running the following command.
npm config set prefix /usr/local

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.

Use global npm or not

When and why I have to use -g when install new module?
I use symfony and i have create Bundle for node and install node_modules inside bundle with (for example cd ../mybundle, then)
npm install felixge/node-mysql
when and Why I have to do something link
npm install -g felixge/node-mysql
thanks
From http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/
In general, the rule of thumb is:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
The -g flag installs packages on your user profile whereas not having this puts it in your current folder. The difference is the scope of the package.

Categories