Nodejs and raw-socket package - node.js

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

Related

node js and xampp conflicts

I run an apache server on localhost using XAMPP and this will mostly work with no problems.
Ocassionaly when I run npm install on a project to install it's dependencies, my connection to the apache server is disrupted, I am unable to access any webpages, I cannot turn off my apache and my machine will not even shutdown and I have to hard reset.
Does anybody know what is happening here and how I can
a) get myself out the situation when it occurs
and
b) prevent it from occurring
I've tried to kill the node processes using pkill node with no success.
Once I hard reset the machine I no longer have any issue.
I believe my problem to be becuase of the fact that I run npm install from inside my htdocs folder which is where apache will usually run from so the obvious solution would be to not do that but does that mean that if an project folder I want o use runs with node that I cannot use xampp?

CentOS8/RHEL8 nodejs 4.9.1 "operation not permitted" when running as non-root user

I'm left with an age old NodeJS code written and compatible with modules and NodeJS v4.9.1.
It's hosted on RHEL6 and it works fine.
Now I'm asked move the code to RHEL8 machine. Got ton of errors when executed tried launching app with both NodeJS 10 and 12.
I downloaded the tar file of NodeJS 4.9.1 and extracted and tried to launch the app and it works fine.
But this is as root.
When I created a systemd service with the following line service file and try to launch it I get "-bash operation not permitted" error
[Unit]
Description=Node App
After=network.target
[Service]
Type=simple
User=node-user
ExecStart=/opt/nodeJS/bin/node /opt/nodeapp/server.js 1> /opt/nodeapp/log/node.stdout.log 2> /opt/nodeapp/log/node.stderr.log
[Install]
WantedBy=multi-user.target
When I switch to the node-user and try to run the basic node -v command I still get the same error.
I even tried with 777 permissions on nodejs direcotory.
Everything works fine as root.
Any suggestions, greatly appreciated.
Ran into a similar version of this issue today (visual studio code remote-ssh extension failing, -bash operation not permitted error when trying to run node binary in the .vscode-server folder)
I also had hardening going on in my Centos8 VM, in this case the DISA STIG. After some digging around in the STIG settings, I found that the fapolicyd service (application whitelisting) was the culprit.
You can check if this is the case for you by uninstalling fapolicyd and seeing if node works after that:
sudo dnf remove fapolicyd
If that works, then you can either just leave it uninstalled or you can reinstall it and add exceptions to it:
sudo fapolicyd-cli --file add /path/to/node/binary --trust-file node
sudo fapolicyd-cli --update

Run node commands as root in electron app

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.

Running mongodb in production

I am currently building a web app using express.js and mongodb. My problem is I do not know how to run mongodb itself in production. Because in the development, I used mongod command, but it need the terminal to be open so mongodb will run. How can I do this in the real server? In my express app, I just use pm2 to run it.
Since you are not an expert on managing this, I would suggest you to use some mongodb-as-a-service, so that they manage that for you and you just simply access it. for example: https://mongolab.com/
Or you can use https://www.mongodb.com/cloud so that you can just lunch a few commands in your server and mongodb+backup will be installed in your server and you can update, or make clusters simply using their interface.
Or if you want to install it in your server, guessing you are using linux, and probably an ubuntu based distro you can run mongod like #aiobe suggested: sudo service mongod start

Error with Mongo starting Meteor

I am using a fresh Linux install. I am trying to install Meteor. Using Ubuntu 12.04, Centos and Ubuntu 13.04. I installed Node.js, Meteor and Meteorite.
Error:
Unexpected mongo exit code 100. Restarting.
Unexpected mongo exit code 100. Restarting.
Unexpected mongo exit code 100. Restarting.
Can't start mongod
MongoDB had an unspecified uncaught exception.
This can be caused by MongoDB being unable to write to a local database.
Check that you have permissions to write to .meteor/local. MongoDB does
not support filesystems like NFS that do not allow file locking.
I've tried:
Deleting .meteor/local/db/mongod.lock
Also I tried to change the permissions for the whole project with chmod.
Do you recommend any Ubuntu distribution?
Thanks in advance.
I have run in this problem before (after meteor update or abnormal meteor termination) and these solutions worked for me before.
Restart your machine.
Delete the file .meteor/local/db/mongod.lock and run meteor again.
Execute meteor reset and start again, but this command will erase your database.
Hope it helps.
It some time depends on your locale settings.
Meteor reset and delete mongod.lock didn't help in my case.
I've fixed it by running
export LC_ALL=C
you can add this line to
sudo vim /usr/local/bin/meteor
All I had to do was run "killall mongod" in the terminal. After that it worked again. I believe it may have cleared parts of the database though.
For me a combination of the suggestions worked out. The answer that has gotten 2 down votes! and this one from another page.
sudo vim /usr/local/bin/meteor
export LC_ALL="en_US.UTF-8"
then make sure which port your mongodb is running at by issuing the following:
meteor mongo
You will get a response like this:
connecting to: 127.0.0.1:3001/meteor
now that you know the port on which the mongo is configured, add the following to the same file we have been editing:
export MONGO_URL=mongodb://127.0.0.1:3001/meteor
Now you are good to go. Of course "meteor reset" works but if you dont want to loose your data, stick with those two export lines.
I have found that setting MONGO_URL helps avoid issue
export MONGO_URL=mongodb://localhost:27017/your_db
If you're having this issue running Ubuntu inside Vagrant/VirtualBox, then the problem come from working in the synced vagrant folder.
The workaround is to initialize the .meteor directory in the home directory and to mount it in the synced folder. Assuming your meteor app is called MyApp and the /vagrant is the synced folder, here's how to do it:
cd ~
meteor create MyApp
cd MyApp
meteor
cd /vagrant/MyApp
sudo mount --bind ~/MyApp/.meteor/ .meteor
meteor
I had a similar issue. It was due to the fact that my project directory was part of my Dropbox and there was some conflicted files in the .meteor/local/db directory. Removing these solved the problem.
I encounter the same issue when my disk space ran low.
Freeing up some space in the disk fixed the problem for me.
I encountered this problem when I ran meteor in VMWare, ubuntu 14.04 guest on a Windows 7 host. The problem was that I was running meteor/mongo in a shared directory. When I used a non-shared directory (~/meteor/project) the problem disappeared.
Following the steps that #Oscar mentions further up should sort you out.
However in my case meteor reset wouldn't work, telling me this:
reset: Meteor is running.
This command does not work while Meteor is running your application. Exit the running Meteor development server.
For which I tracked the process down, like this:
$ ps aux | grep meteor --color=auto
which gave me this info:
$ myUser 71981 0.5 0.0 2849208 3644 ?? S Mon02pm 4:25.27 /Users/myUser/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/mongodb/bin/mongod --bind_ip 127.0.0.1 --smallfiles --port 2001 --dbpath /Users/jgt/Documents/Projects/todos/.meteor/local/db --oplogSize 8 --replSet meteor
As you can see I'm on Mac, but this works fine on any Unix based OS.
So then you only need to kill the process by it's id, like this:
$ kill -9 71981
And finally run again meteor reset
That's it, got it working now. Hope it helps.
One option is to wait until you have your app on a staging or production server, and then test it on an Android emulator provided by Android Studio or BlueStacks.
I am having this issue on WSL for Ubuntu on Windows. The only solution that worked for me was creating a remote mongodb with mLabs and ran the following
export MONGO_URL=mongodb://username:password#randomurl.mlab.com:port#/project
Recently I got the same case, which I run meteor on WSL for Ubuntu on windows 10. I resolved this case by updating WSL to version 2.
Note :
Make sure your Windows build is higher than the minimum WSL 2 requirements. And don't forget to set version 2 as WSL default version after updating.
wsl --set-version [linux_dist] 2
For windows 10, I moved all my Meteor projects in drive C. So far working.

Resources