Run node commands as root in electron app - node.js

I am trying to write electron program for creating and configuring multiple Apache websites sites. But my problem is that i need to read from and write to files that are owned by root user. such as /etc/hosts, etc/apache/sites-avaliable/mysite.conf. my program works only when I run it as root from terminal. but when i package it. I cannot launch it as root user. there as some pakages as sudo-prompt - npm. but they accept only terminal commands. But I want to use native node functions for accessing file system such as
fs.writeFileSync(`/etc/apache2/sites-available/${serverName}.conf`, template);

Yes, it is possible via electron.
You can use https://github.com/jorangreef/sudo-prompt to exec a command using sudo and show a graphical OS password prompt.
You can use sudo-prompt from Electron as well as from any Node script.
It is regularly updated, has no dependencies and does not require any native compilation.

Related

How to install Node Version Manager(NVM) without admin rights

I have no admin rights in my windows machine. Can I install NVM without admin rights? I tried using the environment variable path setup, but its not working in my case.
I have the same need and couldn't find one, so I created one base on another simple nvm:
https://www.npmjs.com/package/#jchip/nvm
Requires powershell 4+ and permission to execute scripts.
(You're talking about https://github.com/coreybutler/nvm-windows right?)
Whether you can install it without admin rights aside, the actual act of switching node versions with it requires them so you're going to have trouble.
Your best bet is to install different versions of node into different paths manually, and then configure your environment variables to point to the right one whenever you need to use it.
eg. prefix your cmd script with PATH=C:\node\v10;%PATH% to have any node or npm calls in that script use whatever node is sitting in v10
try this
create a bat file like below
#cd C:\Users\testuser\AppData\Roaming\nvm
#SET PATH=C:\Users\testuser\AppData\Roaming\nvm\v14.21.1;%PATH%
cd c:\users\testuser\Desktop\Project
#cmd.exe /K
Run bat file and type
code .
It's open with VSCode
go to the terminal and type node and you can see the node version that you set in the bat file.
enter image description here
You can apply any node version as above bat file
If you use Git Bash on Windows, you can add this to your bash.bashrc to switch node versions:
export PATH=/c/path/to/node/dir:$PATH
Then just restart your terminal to pick up the updated PATH.
It will prepend your path with your desired node version. It's the only way I've found to override the installed node version if you don't have admin rights on your machine.

Nodejs and raw-socket package

I have a nodejs application that should start when I turn on the machine (I'm using Linux Ubuntu 16.04 so I put it in the rc.local). my application uses the raw-socket package.
Since my application cannot run as root, in the rc.local, I set:
sudo -u myuser node myapp.js &
when my application calls "new raw-socket", I got an error:
Operation not permitted.
I tried different methods such as "setcap" but they don't work for me.
Furthermore, if i try to run my app after the operating system is loaded, the app works perfectly without any problem....
should I have to add something in the rc.local....did I miss something?
node-raw-socket needs root access to create its raw sockets.
You will have to run
sudo node myapp.js

React create app hot reload is not always working on linux

I created a react application using create-react-app boilerplate, which seems to be very popular, hot reload some times updates when any of the files changes and some times not, seems like there is a minimum duration or something like that, I'm using Ubuntu, node version 7.0, the script in package.json is npm:'react-script start' what I am missing ?
When npm start doesn’t detect changes, below are the common troubleshooting steps provided in the create-react-app documentation - link.
While an app is running with npm start and updating code in the editor should possibly refresh the broswer with the updated code.
If this doesn’t happen, try one of the following workarounds:
If the project files are directly synced to your local system from a cloud storage like Dropbox or Google Drive and you're trying to run the app in them directly, try moving it out.
Due to Webpack bug, you may need to restart the watcher. If the watcher doesn’t detect the index.js and you’re referencing it by the folder name.
Safe write feature in editors like Vim and IntelliJ currently breaks the watcher. You will need to disable it.
Due to Webpack watcher bug, projects with path contains parentheses causes issue, try moving the project to a path without them. .
To allow more watchers in Linux and macOS, you might need to tweak system settings.
If the project runs inside a virtual machine such as (a Vagrant provisioned) VirtualBox, create an .env file in your project directory if it doesn’t exist, and add CHOKIDAR_USEPOLLING=true to it. This ensures that the next time you run npm start, the watcher uses the polling mode, as necessary inside a VM.
Could try increasing max_users_watches- link
More references:
Issue Tracker 1
Troubleshooting webpack
Issue Tracker 2 - Webpack polling
Try these:
Turn off safe write in your IDE
Increase max_user_watches
Your path should not have parentheses
as last resort try to use this as your env variable:
CHOKIDAR_USEPOLLING=true npm start
Sources:
https://github.com/facebookincubator/create-react-app/issues/659
https://github.com/facebookincubator/create-react-app/issues/1049#issuecomment-261731734
I was able to make this work using:
sudo npm start
run this command
sudo -i
echo 1048576 > /proc/sys/fs/inotify/max_user_watches
exit
try deleting the node_modules folder and reinstall
by using cmd npm install
worked for me ( ubuntu 18.04.3 LTS )
In unbuntu, i basically kill all the process running on port (for react app default is :3000).
List all the process running on port 3000.
lsof -i :3000
This command will show something like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 7429 yipl 19u IPv4 1081760 0t0 TCP localhost:3000->localhost:35762 (ESTABLISHED)
chrome 26448 yipl 177u IPv4 1080082 0t0 TCP localhost:35762->localhost:3000 (ESTABLISHED)
Now kill the process by PID.
kill -9 7429
kill -9 26488
Start your react app again.
Apparently hot module reloading only works out-of-the-box if you eject your app.
But if you haven't ejected your app, then you can follow these instructions to get it working.
Find the ReactDOM.render(...) at the top of your app, and add these lines below it:
ReactDOM.render(...);
if (module.hot) {
module.hot.accept('./App', () => {
// --- Copy-paste your usual ReactDOM.render(...) call here: --- //
ReactDOM.render(...);
});
}
The instructions linked above also show how to hot reload things outside of the component tree, such as redux reducers.
One additional case I just experience is when using multiple version of nodejs with NVM in parallel. Basically, I have two terminal windows, one run node 10.x, other on node 9.x, and Webpack watcher stops seeing change.
The solution is to bring both to the same node version
sudo npm start
i dont know more about linux, but it is probably due to firewall settings, as i have done the same in windows and there to start react dev server we need to give the access through firewall, so to do the same here we need to give it the admin permision(sudo in case of a ubuntu linux distribution).
If you start your app with a bunch of errors it seems to stop watching your files and thus will not detect your fixes to the broken files. It seems as if you need to initiate "start" with not too many/severe errors for the file watcher to work.

NodeJS installation to jenkins : npm not found

Maybe someone can tell me what I missed installing nodeJS to jenkins, because when I want to execute shell script npm update I get error npm: not found.
I installed jenkins nodejs plugin then added installer:
After I created job with prams:
What I need to do more, to have accessible npm in any jenkins job?
It works for me. The only difference is that where you have a text box that reads "0.12.7", I have a popup menu that reads "Node.js 0.12.7". Maybe something has gone wrong and you should delete the installer and add it back?
Windows users, did you restart?
Do keep in mind that you need to restart active processes after installing npm.
During installation the folder of your npm.cmd is added to the PATH variable. However, active applications don't refresh their PATH variable automatically. That also holds for your active Jenkins service (and its child processes). They are still running with an old version of your PATH variable.
In other words, you need to restart the Jenkins service (or restart your entire system). Once restarted, Jenkins will have an updated PATH variable, and it will be able to find the npm.cmd processes and others.
What's wrong with using absolute paths?
Using the full path is not a solid solution. A package.json file can contain references to other cli processes (e.g. node, ng, react-scripts, npm-run-all, ...). You will probably have similar issues there. There is no way to keep up and reference all of them with full paths.

Nodejs is not available outside of its installation directory

I installed nodejs 64bit on my windows8.
NOTE: By install, I mean I used node-v0.10.35-x64.msi andn its Windows8 for SurfacePro3.
I am not familiar with add PATH. What exactly should add I add and how I run it after I added it?
However, nodejs will not be available outside of its own installation folder.
For example,
if I installed it at /www/test directory, then it will not available either in www or any level after test, like, /www/test/anothertest. Only /www/test will have node running.
I test it by write node -v on /www/test directory in command prompt. I consider its available when it returns me the node version number. When it is not recognized, it will say 'node' is not recognized as an internal or external command...'
I insta
Restart your computer always fix everything....
Yes it did now everything works lol

Resources