React create app hot reload is not always working on linux - node.js

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.

Related

How can nodemon be made to work with WSL 2?

Ever since updating from WSL 1 to WSL 2 with the Windows 10 April 2020 update (and thereafter updating Ubuntu 18 to Ubuntu 20), I have not been able to get nodemon to hot reload when there are file changes in the project's directory. When I make any changes to .js files, there's no restarting of the server or output at the terminal:
I start my Node.js server with nodemon like this:
NODE_ENV=development DEBUG='knex:*' nodemon --verbose --inspect ./server.js"
And in case its useful, here is my server.js:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server started and listening on port ${PORT}`);
});
I am not even sure how to troubleshoot this further to get more useful information about what's going on.
Root cause:
inotify is not fully supported in the 9P filesystem protocol on WSL2.
There are several github issues on the WSL project related to this, but perhaps the most relevant is #4739.
Possible Workarounds:
Try nodemon -L (a.k.a. --legacy-watch) as Simperfy suggested.
Try running from the default ext4 filesystem (e.g. mkdir -p $HOME/Projects/testserver). Note that a symlink to the Windows filesystem will still not work. As a bonus, the WSL ext4 filesystem will be much faster for file intensive operations like git.
You can still access the source from Windows editors and tools through \\wsl$\.
Use Visual Studio Code with the Remote-WSL extension to edit your source on the Windows filesystem. The easiest way to do this is by navigating in WSL to your project directory and running code ..
Visual Studio Code's WSL integration does trigger inotify for some reason.
Downgrade the session to WSL1 if you don't need any of the WSL2 features. I keep both WSL1 and WSL2 sessions around. The best way to do this is to create a backup of the session with wsl --export and wsl --import. You can switch the version of a WSL distro at any point with wsl --set-version.
I did test this on WSL1 with a sample project under the Windows filesystem, and editing via something as basic as notepad.exe under Windows still triggered nodemon to restart.
Longer answer:
nodemon worked "out of the box" for me on WSL2 on the root (/) ext4 mount (e.g. $HOME/src/testserver).
It also worked correctly when I tried it under the default /mnt/c mount that WSL/WSL2 creates. Of course, /mnt/c is much slower under WSL2. Edit - It turns out that I was using Visual Studio Code when I attempted this. Editing from other Windows apps on the Windows filesystem did not trigger nodemon to restart.
But looking at the first line of your screenshot, I see that you are running this from /c/Users/.... I'm thinking maybe you created this (perhaps CIFS) mount to try to work around the WSL2 performance issues - It's a common workaround.
I didn't set up a CIFS mount, but I was able to reproduce your problem by mounting with (substituting your Windows username):
mkdir $HOME/mnttest
sudo mount -t drvfs 'C:' $HOME/mnttest
cd $HOME/mnttest/Users/Raj/Projects/testserver
Running nodemon from this mount failed in the same manner that you describe -- Changes to the source did not trigger a restart.
However, running with nodemon -L on this mount did trigger a restart when source files were changed.
It also may be possible to fix the problem by mounting with different options, but I'm just not sure at this point. Edit - Seems unlikely, given the bug reports on this on Github.
Also, you may want to create some exports/backups of your WSL sessions. It's too late at this point (for your previous install), but you could have run wsl.exe --export to create a backup of the Ubuntu 18.04/WSL1 filesystem before upgrading. You can also change the version of a particular distribution with wsl.exe --set-version. This could give you some better "before/after" test comparisons.
I am using the WSL 2 and I solved the issue by adding the following env variable: CHOKIDAR_USEPOLLING=true.
This is how looks like my nodemon command:
CHOKIDAR_USEPOLLING=true nodemon index.js
Now you can keep WSL2 instead of moving your environment to WSL1.

WebStorm Node.js debug with Docker

I'm trying to debug a Node.js script with WebStorm 2019.3 and Docker as a remote Node interpreter. So far I can start the script, debug it, but any changes done on local do not trigger a nodemon restart of the script inside the Docker container (files inside the container ARE actually changing, I've checked).
Any ideas? I'll attach the WebStorm run config.
I think there is something wrong about the way that I'm using nodemon when starting the script, but I have no idea how to fix it for WebStorm config.
Looks like you might need to enable legacyWatch.
According to the documentation:
In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enables Chokidar's polling.
Via the CLI, use either --legacy-watch or -L for short: nodemon -L

Missing file or plugin on freshly installed Cloud9 ide?

I'm currently trying to set up cloud9 on my debian server for node.js development. I followed some simple steps as described below, but there seems to be missing something and I would like to know how this can be resolved in the best way. Of course, I could start looking for the missing file on the web and put it there, but I think that's not the way to go.
What I did is this:
ssh'ed into my Debian server into my home folder
sudo git clone https://github.com/c9/core.git sdk (completed succesfully)
cd sdk
sudo ./scripts/install-sdk.sh
The installation finishes like this:
Success!
run 'node server.js -p 8080 -a :' to launch Cloud9
When I then start up the IDE with this command:
node server.js -p 1234 -l 0.0.0.0 -a rob:mypass -w /home/rob/myproject/
...everything looks good:
Starting standalone
Connect server listening at http://x.x.x.x:1234
Using basic authentication
CDN: version standalone initialized /home/rob/sdk/build
Started '/home/rob/sdk/configs/standalone' with config 'standalone'!
Cloud9 is up and running
and I can basically reach the IDE from my local browser. The problem is that it stops on the welcome screen with this loading indicator in the middle right under the cloud9 logo. The JS console shows a missing file (I've replaced my real URL):
http://myserver.de:1234/static/plugins/c9.ide.collab/notifications/bubble.js Failed to load resource: the server responded with a status of 404 (Not Found)
I then looked into the folder /home/rob/sdk/plugins/c9.ide.collab/, but it is empty.
What am I missing? Do I need to install something with npm or do I have to change the package.json file somehow? I'd really appreciate some help because I think wildly modifying and copying in files won't make things better.
edit: It turns out it was "bug in the script updating references [of some plugins]". Details: https://github.com/c9/core/pull/365
—Cheers!
Original answer:
I think think the c9.ide.collab plugin's refspec is wrong here: https://github.com/z3cka/core/commit/5f45ea38f422160a0030c5ef8d1efb19231bc78c#diff-b9cfc7f2cdf78a7f4b91a753d10865a2
I changed it back to #54aa1cbee0, ran the install-sdk.sh and it grabbed the latest version of the collab plugin. While prepping a PR, I just found 3 more plugins with bad refspecs
c9.ide.language
c9.ide.language.javascript.eslint
c9.ide.run.debug
I'm currently working on a fix for the rest of these.

How can I run a node.js server

I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux

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