while installing node.js getting permission denied error - node.js

I downloaded the file for version 8.9.2 of node js and I am using lubuntu. I extracted the tar.xz file of nodejs I downloaded into a folder I made, and then in my command line entered
cd /usr/src
wget http://nodejs.org/dist/v8.9.2/node-v8.9.2.tar.gz
But then this shows up.
--2017-12-07 22:08:56-- http://nodejs.org/dist/v8.9.2/node-v8.9.2.tar.gz
Resolving nodejs.org (nodejs.org)... 2400:cb00:2048:1::6814:172e, 2400:cb00:2048:1::6814:162e, 104.20.22.46, ...
Connecting to nodejs.org (nodejs.org)|2400:cb00:2048:1::6814:172e|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 31109143 (30M) [application/gzip]
node-v8.9.2.tar.gz: Permission denied
Cannot write to ‘node-v8.9.2.tar.gz’ (Permission denied).
So how do I install it properly?

As Kishor Pawar already mentionet:
The folder /usr/src belongs to the user root and your current user does not have write access (you can see that by using ls -al /usr/src)
In order to install it you have multiple Options:
use linux package-manager.
Install it via sudo apt-get install package=version (example)
use a wrapper like nvm (https://github.com/creationix/nvm)
installing it manually: the user that uses npm needs to have write access to all folder that are used by npm. So in case you want to install it to /usr/src your user needs to be a sudoer. Else install it into your homefolder(i am not sure if this is sufficient)
Option 2 is my preferred option for a developing machine. You can easily install specific version and have them in parallel and switch versions as you need. Easy to use and easy to upgrade. No hassle on access privileges, as it puts all dependencies to the users home folder.

You will have to be root or have use sudo before command provided you are added to sudoers group.

Related

Command is not found when ran with sudo

I am trying to change the version of nodejs using cmd n installed globally by yarn. Neither yarn is detected by sudo nor n.
Please help me to fix this:
When running the n command to change node version, it shows permission is denied by many files to be written by n
Two approaches to avoid permission issues without requiring use of sudo when running n:
install and run Node.js from your home folder using N_PREFIX and PATH (see also n-install which automates this)
if it is your personal computer, take ownership of the folders used with default install location of /usr/local
See the project README for more: https://github.com/tj/n
(Disclaimer: I am the maintainer of n.)

How to install node and npm on macOS without admin rights?

I work in a big organisation as UX designer. I have basic programming knowledge. I've been trying for ways to install node / npm on macOS without admin rights with no luck.
I've tried all the methods described here
https://gist.github.com/isaacs/579814
but all of them require using sudo or make which I cannot use
I've also downloaded the installer from nodejs.org and, unpack the file with pkgutil but when I run tar on the payloads they are copied to usr/local/bin/includes/node then I try to change the path in the host file but I need admin password to save.
I thought that this would be as easy as doing it on a Windows machine but it's not.

Run npm as superuser, it isn't a good idea?

I'm getting errors with npm while trying to install/update packages without SU permissions on Linux.
The easy way to solve the problem is execute sudo npm install <package>, but I'm not sure if it is a good idea.
Best way is to become the owner of .npm folder, as I found into StackOverflow's questions and blog posts.
My question is: why run npm as SU it isn't a good idea?
Running npm as a super user has a risk of running some untrusted code as a super user which can potentially mess with your entire system. Running npm as an unprivileged user has a risk of running that code with less privileges and it won't be able to mess with the entire system - just with your own files (which can be equally bad, depending on how you look at it).
What I often do and recommend is to install Node in your home directory instead of globally on the system if it's your own computer. That way you don't have to run with sudo or su for npm or even for make install of Node itself.
I run a lot of versions of Node that I compile from sources sometimes with different switches and the convention that I use is to install Node in versioned directories, either globally in /opt (but then you need sudo) or locally in my home directory in ~/opt.
I do it like this:
wget https://nodejs.org/dist/v7.1.0/node-v7.1.0.tar.gz
tar xzvf node-v7.1.0.tar.gz
cd node-v7.1.0
./configure --prefix=$HOME/opt/node-v7.1.0
make && make test && make install
Then I create a symlink ~/opt/node pointing to ~/opt/node-v7.1.0 and I have:
PATH="$HOME/opt/node/bin:$PATH"
in my .profile or .bashrc.
That way I don't have to run as super user for installing Node or for running npm.
As a bonus I can quickly switch my default Node version just by changing the symlink, and at any time I can run any other version if I change the PATH or run Node with a full path like ~/opt/node-v7.0.0/bin/node.
I explained that installation process in more detail in my other answers:
node 5.5.0 already installed but node -v fetches with “v4.2.1” on OS X & homebrew?
NodeJS API with external deps in other language
I don't want to go into too much detail here since this answer is about why running npm as a superuser is not a good idea - this installation process is just one solution to not have to run npm as a superuser.
Other options of setting your npm permissions to avoid running as a superuser are described in Fixing npm permissions in npm docs (thanks to RyanZim for pointing it out in the comments).

node user directory leading to command not found

I suipidly ran this script to stop having to use sudo on npm -g commands and now my node_modules are located at:
/Users/myusername/.npm-packages/lib/node_modules
when trying to run a commands i.e yo bower i get -bash: bower: command not found
Do I need to link this directory to the usr/bin?
In trying to sort this out I have run lots of commands and created symbolic links, I feel like my system is a real mess and I cannot really see what is going on? Help to ensure my system is clean would be really helpful.
Generally, it's a good idea to ensure that you do not need admin rights to run npm commands. The problem you're having, is that the directory where npm now installs its commmands (/Users/myusername/.npm-packges/bin) is not in your PATH. Your PATH is a list of directories where your shell searches for the commands you type.
You can add the directory to your path by adding the following line to the .bashrc file in your home directory (create it if it doesn't exist).
export PATH="${PATH}:/Users/myusername/.npm-packages/bin"
Open a new terminal, and bower should work again.
As an additional tip, I believe you're running on OS X? When installing Node.js using Homebrew, it is automatically installed in a proper way (no sudo needed to install packages globally and they will just work). Next time you're installing Node.js (or something else), Homebrew might be worth a try.

NodeJS and npm without sudo on CentOS

On my box I have the Node and NPM binaries installed under /opt/node/bin. And the path is added properly.
node -v
and
npm -v
work fine. When I create a file and run it with node all works as expected. However, when I run:
npm init
the program fails to write package.json because it does not have write permission. I use:
sudo npm init
I get a file with owner and group of 0 0 and so any regular users cannot modify this file. I don't want to have to chown every file node/npm generates.
Is there way to get node/npm to run as a user in the same group as my other users and have write permissions to the same directories?
You could simply chown your /opt/ directory and future calls to npm init will be owned by you. Better yet, work in your /home folder and make sure npm and node are in your $PATH. This way you don't need to worry about permissions for initializing a new node module. You will, however, need to use sudo to install packages globally. This is bad practice according to the maintainer of Nodejs:
http://howtonode.org/introduction-to-npm
I would follow along with his setup there. As he mentions, its very dangerous to give root access to a package manager.

Resources