Using SUDO with NVM? - node.js

How is it to execute something like sudo npm rebuild or sudo node if Node.js was installed by NVM ?
Each time I type sudo command, my console tries to execute npm or node program in /usr/bin/ or /usr/lib, neither of which do exist.

Try creating symlinks in /usr/bin or /usr/lib. Not a strong solution, but may be enough.
sudo ln -s /home/denny/.nvm/versions/node/v0.12.0/bin/npm /usr/bin/npm
sudo for writing in system location. Suggestions here may happen useful, especially rvmsudo or group access.
Other suggestion:
According to comment, you may try to create a bash script at /home/denny/npm.sh:
#!/bin/bash
PTH="/home/denny/.nvm/versions/node/$(node -v)/bin/npm"
$PTH
chmode it +x properly and create symlink with this:
sudo ln -s /home/denny/npm.sh /usr/bin/npm
Should always check for node -v and run npm from proper directory.

I needed to do this to run the Atom installation script, but I didn't want to permanently change my machine setup. So, I did:
sudo bash
and then I sourced the nvm.sh file in my user's home directory tree:
source /home/ada-lovelace/.nvm/nvm.sh
That let me run the necessary commands with root. Of course, this means the very next time I need to run node in a session with root I'll have to do these steps again, but I don't anticipate needing to do this in the near future.

Related

Where is the npm directory supposed to be?

The Node version is 17.7.1. When I run npm -v I get:
bash: /usr/local/bin/npm: No such file or directory
But when I run npm root -g I get:
/usr/local/lib/node_modules
Running npm list -g also gives me the same message as running npm -v.
So, where is it supposed to be and how can I fix this?
There are a few issues as to why this is occuring.
It appears that your computer cannot find the following path (below) to the npm folder, but it can find the global node_modules folder.
/usr/local/bin/npm
This may occur for a number of reasons. Luckily, there are many solutions to these issues.
1. Reinstall Node.js and npm^
The first solution is pretty obvious, and it is to reinstall Node.js and npm^.
The installation process is very simple.
If you are using only the command line, you can install Node.js using the following command. The npm command comes installed with Node.js.
$ sudo apt-get install nodejs
Or, if you want to use the installer, download and run the installer from nodejs.org.
Check the versions of Node.js and npm to see if it has worked.
$ node -v
$ npm -v
The helpful thing about reinstalling Node.js and npm in this case is that, if Node.js and npm weren't installed in the first place (highly unlikely), it will install them.
However, if they were already installed, but then something happened to the installation directory (e.g. it was renamed, moved, deleted or corrupted), then the new installation will most likely have fixed that issue.
2. Add /usr/local/bin/npm to the path (solution worked for OP)
If the first solution didn't work for you, then the next issue that might be occurring might be that /usr/local/bin/npm isn't in your path, which means that bash cannot find it.
To add npm to your path, follow the steps below.
Normally, to get the path with npm, you need to run the following command.
$ npm bin
Obviously, we have to use a workaround. But, we can just use an assumed path for where the binaries of npm are stored, which is...
/usr/local/bin/npm/bin
However, this may vary, so, to be sure that this exists, you can simply type it into the command line.
$ cd /usr/local/bin/npm
Then, you can run the ls command, and see if there are any bin folders or .sh shell scripts that might be used for the path.
$ ls
Make sure you save the binary part for later!
Open the home directory using the following command. This is because the .bashrc file is in the home directory (used in the next step).
$ cd $HOME
Open the .bashrc file using vi (or any file editor of your choice, but vi will be used in this example).
$ vi .bashrc
Then, press the i key for Insert Mode. Type the following in the file.
export PATH=/usr/local/bin/npm/bin:$PATH
In vi, press Esc, then press the following keys: :, w, q, !, Enter.
:wq!
The file is now saved and the editor is closed.
Normally, the .bashrc file is only read when you log in each time. To avoid doing that, you can force bash to read the .bashrc file and update itself. To do this, run the following command.
$ source .bashrc
The bash shell should now be restarted.
These were quite a few steps (four, but many were compressed into one), but if a situation occurred where for some reason npm wasn't adding itself to the path, this solution should fix it.
3. Denied read/write access
Another issue is that your user profile doesn't have the right permissions to read/write to usr/bin/local/npm, or you didn't give npm permissions.
The fix to this solution is very simple.
Run the npm commands you want to run using sudo.
$ sudo npm -v
$ sudo npm list -g
If you want, you can make the commands you run automatically run with sudo.
$ sudo -s
$ npm -v
$ npm list -g
Warning: If you don't have the password for sudo, you can't run any of the commands using sudo above.
This should fix your issue, if denied read/write access is the problem.
There are most likely many more solutions^^, but they might be added later.
^ This is a very usual process, but it has to be added here so that I can be sure that this solution has been tried.
^^ These solutions may be found later and edited to this answer. Edited once.
use this command :
npm list -g
you should see your npm directory at first line

How to reverse "sudo chown -R 501:20"

Ran npm i -g {library} inside my project and npm gave me an error because I didnt use sudo. instead of using sudo on a second attempt, I followed npm's suggestion to run sudo chown -R 501:20 "/Users/vorousjames/.npm" (inside my project).
now anytime I run an npm command (anywhere), I get bash: /Users/vorousjames/.npm-global/bin/npm: No such file or directory
How do I fix this so I can use npm again?
try running this in the terminal
export PATH="/Users/vorousjames/.npm:$PATH
if it works add that to your .bash_profile

why do I have to write Sudo every time in ubuntu

I'm creating a react app, using command
create react-app new-app
but it's not creating a new app until i change this command to
sudo create react-app new-app.
After this if I try to install any dependency using npm then also I have to include sudo in the beginning of every command.
And also sometimes the project files are not accessible by code editor as they get locked .
please help me to get rid of this sudo ...
Sudo used to stand for "Superuser do" It simply means that your operating system thinks that you don't own the directory. You can use this command to resolve the issue: chown [username]:[username] -R. chown is short for "Change Owner".
Here's a trick you could use in the future - incase you have to execute the previous command using sudo you can punch in sudo !! and this will run the previous command as root.
So how that would work is:
some command - Doesn't run and you need sudo.
sudo !! - executes sudo some command automatically.
Best wishes :)

Running electron as root on Linux

I am making an application with electron which uses the wiring-pi library. This needs access to the GPIO on my Raspberry Pi, which requires root.
When I run electron . in the folder, the app opens fine but then says (in the terminal):
wiringPiSetup: Must be root. (Did you forget sudo?)
However when I try sudo electron ., I get an error:
sudo: electron: command not found
Does anyone know why this is happening?
Also, for the record, the same thing happens when I run npm as root:
pi#raspberrypi:~/rubiks-robot $ sudo npm
sudo: npm: command not found
Any ideas of how I can fix this issue and run Electron as root?
it looks like a problem of environment variables. The environmnet variables are set for your user but not for root.
Try to ship your variables with the "-E" switch of the sudo command:
sudo -E command
Please try to see here for other similar questions
How to keep Environment Variables when Using SUDO

Makefile install copy executable to usr/local/bin Linux

In my Makefile I'm trying to copy the executable file into usr/local/bin.
install:
sudo cp program1 usr/local/bin
My Makefile and program1-file is in a directory src in Documents so this doesn't work. I probably need the whole path from my src directory.
Is there a general way to make it work regardless of where I put my directory with the Makefile and executable? Maybe using the PATH variable or something?
usr/local/bin is a relative path. If you don't want that, use an absolute path:
install:
cp whatever /usr/local/bin
Some tips:
Don't use sudo in your Makefile, that's unusual. Note in the installation docs that the install target has to be run by a user with sufficient privileges (and people will do sudo make install when they want to)
Look into the install (man install), it's meant for this sort of thing.

Resources