why do I have to write Sudo every time in ubuntu - node.js

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 :)

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

Github actions can not run the config.sh file in the ubuntu 20.04

I am trying to set up the GitHub actions in ubuntu. I made a folder and install the runner using my root account. Now, this is how permissions look like.
When I tried to run
sudo ./config.sh --url https://github.com/user/api --token supersecret
It gives me the error
Must not run with sudo
The solution most people say is that export RUNNER_ALLOW_RUNASROOT="1" and then run the command. But this widely accepted solution is not working for me for some reason.
And some others say create a non-root user and try to run. I tried that way too. It ends up with more errors.
How do I fix this?
I did it this way to run env ❯ sudo env RUNNER_ALLOW_RUNASROOT="1" ./run.sh
for the config just keep the same pattern, put the env at the beginning.

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

mean.io initialising new project - owner rights

I'm trying to initialise my first MEAN project with command mean init myProject. This command returns the error :
There are 1313 files in your ~/.npm owned by root
Please change the permissions by running - chown -R whoami ~/.npm
By running this chown command I have another error Operation not permitted.
I tried to use nvm but I'm not sure if it's a good direction. If yes I have no idea how to configure it.
Could you please help me with that, I'm struggle with it since couple of days :/ Thanks
You need to run chown as root. Get your username using
whoami
Then run this replacing <username> with the one you got in the previous step
sudo chown -R <username> ~/.npm
For future reference, don't run "npm install -g" commands with sudo. If you're having permission problems, fix those first.
I had really serious problems with mean.io framework/scaffolding tool.
I know, that isn't solution for your problem but please be so kind to consider yeoman.

Using SUDO with NVM?

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.

Resources